This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const schema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'RootQueryType', | |
fields: { | |
search: { | |
type: new GraphQLList(SearchableType), | |
args: { | |
text: { type : new GraphQLNonNull(GraphQLString) } | |
}, | |
resolve(root, args) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const resolveType = (data) => { | |
if (data.username) { | |
return UserType; | |
} | |
if (data.director) { | |
return MovieType; | |
} | |
if (data.author) { | |
return BookType; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const UserType = new GraphQLObjectType({ | |
name : 'User', | |
fields : { | |
username : { type : GraphQLString } | |
} | |
}); | |
const MovieType = new GraphQLObjectType({ | |
name : 'Movie', | |
fields : { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DATA = [ | |
{ username : 'catherine' }, | |
{ director : 'catherine hardwicke' }, | |
{ author : 'catherine cookson' } | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
graphql, | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLNonNull | |
} from 'graphql'; | |
const schema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query { | |
search(text: "cat") { | |
searchPreviewText | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Searchable { | |
searchPreviewText: String! | |
} | |
type User implements Searchable { | |
searchPreviewText: String! | |
username: String! | |
} | |
type Movie implements Searchable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
union SearchResult = User | Movie | Book | |
type Query { | |
search(text: String!): [SearchResult]! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query { | |
search(text: "cat") { | |
... on User { | |
username | |
} | |
... on Movie { | |
director | |
} | |
... on Book { | |
author |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query { | |
search(text: "cat") { | |
users { | |
# ... | |
} | |
movies { | |
# ... | |
} | |
books { | |
# ... |