-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is proper way to convert GraphQLObjectType to GraphQLInputObjectType? #312
Comments
I'll answer 1 and 3 first.
|
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:
One thing to note - non-nullity has a bit different meaning in |
Here is example code that implements the above algorithm: import { mapValues } from 'lodash';
import {
GraphQLNonNull,
GraphQLScalarType,
GraphQLInputObjectType,
GraphQLEnumType,
} from 'graphql';
export default function createInputObject(
type
) {
return new GraphQLInputObjectType({
name: type.name + 'Input',
fields: mapValues(type.getFields(), (field) =>
convertInputObjectField(field)
),
});
}
function convertInputObjectField(
field,
) {
let fieldType = field.type;
const wrappers = [];
while (fieldType.ofType) {
wrappers.unshift(fieldType.constructor);
fieldType = fieldType.ofType;
}
if (!(fieldType instanceof GraphQLInputObjectType ||
fieldType instanceof GraphQLScalarType ||
fieldType instanceof GraphQLEnumType)) {
fieldType = fieldType.getInterfaces().includes(NodeInterface) ?
ID :
createInputObject(fieldType)
}
fieldType = wrappers.reduce((type, Wrapper) => {
return new Wrapper(type);
}, fieldType);
return { type: fieldType };
} |
@freiksenet |
@freiksenet Can you explain where Also, did you mean |
@johanatan you may use this ready solution: import { TypeComposer } from 'graphql-compose';
const yourConvertedInputType = TypeComposer.create(YourOutputType).getInputType(); |
I have
RefType
and it's widely using in other types:Today I want to pass this type like input field for mutation:
And get error
NotesAddInput.ref field type must be Input Type but got: Ref.
A couple minutes of googling gets answer to useGraphQLInputObjectType
.So I have several questions:
RefType
, which I can use forqueries
andmutations
? MyRefType
is like scalar type, but have two values. Analogous type may beLatitudeLongitudeType
as example.GraphQLObjectType
toGraphQLInputObjectType
? Or I should define two types (one type for queries, second for mutations and may be third in future, when subscriptions come)?PS. If somebody tries to find understanding of a design purpose
GraphQLObjectType
andGraphQLInputObjectType
, you may read https://medium.com/@HurricaneJames/graphql-mutations-fb3ad5ae73c4#.wi9iwkgotThe text was updated successfully, but these errors were encountered: