Example for defining nested object without creating type? #168
-
I remember reading there's an example for this somewhere either in the docs or in the GH issues/discussion, but can't seem to find it. The use-case was around args or inputs that might contain nested objects - rather than defining a separate type for each one, you can use some method to define it within the input itself. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
This is not something that currently exists. GraphQL doesn't have a way to represent nested objects without giving them a type. There were some ideas around creating a method for defining types inline, but there were some issues getting the API to work. Closest thing is the with-input plugin (not merged, open as a PR) but that's not quite the same thing |
Beta Was this translation helpful? Give feedback.
-
I think I was thinking of the simple objects plugin |
Beta Was this translation helpful? Give feedback.
-
For anyone else coming to this discussion, i had a similar desire, and wanted to do something like this query, but separate out the resolvers for query typeaheadQuery($query: String!) {
typeahead {
fruits (input: $query) {
name
}
vegetables (input: $query) {
name
}
}
} Following this discussion, I wound up doing this: const TypeaheadRoot = builder.simpleObject('TypeaheadRoot', {});
builder.queryField('typeahead', (t) =>
t.field({
type: TypeaheadRoot,
resolve: () => ({}),
}),
);
builder.objectField(SearchRoot, 'fruits', (t) => t.field({
type: ['Fruit'],
resolve: () => {
// resolve fruits
return [{ name: 'Orange' }];
},
}))
builder.objectField(SearchRoot, 'vegetables', (t) => t.field({
type: ['Vegetable'],
resolve: () => {
// resolve vegetables
return [{ name: 'Broccoli' }];
},
})) |
Beta Was this translation helpful? Give feedback.
This is not something that currently exists. GraphQL doesn't have a way to represent nested objects without giving them a type. There were some ideas around creating a method for defining types inline, but there were some issues getting the API to work. Closest thing is the with-input plugin (not merged, open as a PR) but that's not quite the same thing