Skip to content

Instantly share code, notes, and snippets.

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
search: {
type: new GraphQLList(SearchableType),
args: {
text: { type : new GraphQLNonNull(GraphQLString) }
},
resolve(root, args) {
const resolveType = (data) => {
if (data.username) {
return UserType;
}
if (data.director) {
return MovieType;
}
if (data.author) {
return BookType;
}
const UserType = new GraphQLObjectType({
name : 'User',
fields : {
username : { type : GraphQLString }
}
});
const MovieType = new GraphQLObjectType({
name : 'Movie',
fields : {
const DATA = [
{ username : 'catherine' },
{ director : 'catherine hardwicke' },
{ author : 'catherine cookson' }
];
import {
graphql,
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull
} from 'graphql';
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
query {
search(text: "cat") {
searchPreviewText
}
}
interface Searchable {
searchPreviewText: String!
}
type User implements Searchable {
searchPreviewText: String!
username: String!
}
type Movie implements Searchable {
union SearchResult = User | Movie | Book
type Query {
search(text: String!): [SearchResult]!
}
query {
search(text: "cat") {
... on User {
username
}
... on Movie {
director
}
... on Book {
author
query {
search(text: "cat") {
users {
# ...
}
movies {
# ...
}
books {
# ...