You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What is proper way to convert GraphQLObjectType to GraphQLInputObjectType? #312
And get error NotesAddInput.ref field type must be Input Type but got: Ref. A couple minutes of googling gets answer to use GraphQLInputObjectType.
So I have several questions:
Does GQ have some object type for my RefType, which I can use for queries and mutations? My RefType is like scalar type, but have two values. Analogous type may be LatitudeLongitudeType as example.
What is proper way convert GraphQLObjectType to GraphQLInputObjectType? Or I should define two types (one type for queries, second for mutations and may be third in future, when subscriptions come)?
May be error message can be improved, because right now it does not help to determine what is wrong?
InputObjectType can only have other input types as it's members. This is because ObjectType might have mutually-recursive dependencies with other types, which can't be resolved to a JSON structure. Thus you can't use Ref directly in InputObjects. Error is just telling you that - ObjectType is not a kind of type you can use as input and thus as part of input object.
Now for question number 2. There are multiple approaches to how to handle this, in some APIs, you might design all your inputs to be custom and thus never convert from object types. Otherwise we've followed the following approach at Reindex:
Go through all the fields of the ObjectType (through type.getFields())
If field is a scalar, add it to InputObjectType as is
If field is a ObjectType
if it can be referenced by an id (in our case, we check if ObjectType has Relay's Node interface), then add reference to it (id) to InputObject
if it is "inline" type, recursively call the conversion function on it, get InputObject and add it
If field is GraphQLNonNull or GraphQLList, inspect the inner type as above, and add result wrapped in corresponding wrapper
One thing to note - non-nullity has a bit different meaning in GraphQLObject and GraphQLInputObject. For input object it means that the field is required, for objects that means that not returning a non-null value for that field is an error. You might, in some cases, have non-null in InputObject, but not in an Object, because null is often a valid return value in GraphQL, eg when permissions are invalid.
Activity
freiksenet commentedon Mar 14, 2016
I'll answer 1 and 3 first.
InputObjectTypecan only have other input types as it's members. This is becauseObjectTypemight have mutually-recursive dependencies with other types, which can't be resolved to a JSON structure. Thus you can't useRefdirectly in InputObjects. Error is just telling you that -ObjectTypeis not a kind of type you can use as input and thus as part of input object.freiksenet commentedon Mar 14, 2016
Now for question number 2. There are multiple approaches to how to handle this, in some APIs, you might design all your inputs to be custom and thus never convert from object types. Otherwise we've followed the following approach at Reindex:
ObjectType(throughtype.getFields())InputObjectTypeas isObjectTypeObjectTypehas Relay'sNodeinterface), then add reference to it (id) toInputObjectInputObjectand add itGraphQLNonNullorGraphQLList, inspect the inner type as above, and add result wrapped in corresponding wrapperOne thing to note - non-nullity has a bit different meaning in
GraphQLObjectandGraphQLInputObject. For input object it means that the field is required, for objects that means that not returning a non-null value for that field is an error. You might, in some cases, have non-null in InputObject, but not in an Object, because null is often a valid return value in GraphQL, eg when permissions are invalid.freiksenet commentedon Mar 14, 2016
Here is example code that implements the above algorithm:
nodkz commentedon Mar 14, 2016
@freiksenet
YAY, many thanks!!!
Definitely, +100 to karma for next brewing ;)
johanatan commentedon Nov 15, 2016
@freiksenet Can you explain where
NodeInterfaceis defined? I scanned the entire GraphQL source (0.8.1) and can't find it.Also, did you mean
GraphQLIDrather thanID? If not, where isIDdefined?nodkz commentedon Nov 16, 2016
@johanatan you may use this ready solution: