Reputation: 1403
I need to return a string or a boolean value??? any ideas?
login: {
type: GraphQLString || GraphQLBoolean, // <------ but it only take one type
description: 'Desc',
resolve: (root, args) => {
// Need to return a string or a boolean
}
Upvotes: 0
Views: 4610
Reputation: 33
You can use a Union for that:
http://graphql.org/graphql-js/type/#graphqluniontype
But as griffith_joel suggested - look closer at what you're trying to do by overloading that one field and think if it would be better off as two distinct fields. Not knowing your intent, but perhaps a non-null "success" field and an optional "username" field.
Upvotes: 0
Reputation: 2132
You can look at custom types: http://graphql.org/graphql-js/type/#example-1. This allows you to create custom types based on your API use-case. I'll throw in that doing this is like a "code-smell" that something might need to be designed and vetted further.
var StringOrBool = new GraphQLScalarType({
name: 'StringOrBool',
serialize: parseStringOrBool,
parseValue: parseStringOrBool,
parseLiteral(ast) => parseStringOrBool(ast.value)
});
function parseStringOrBool(value) {
return (typeof value === 'string' || typeof value === 'boolean') ?
value :
null;
}
You'll have to check this for correctness, but should work
Upvotes: 2