Skip to content

Instantly share code, notes, and snippets.

@Axure
Forked from anonymous/graphql-decorators.ts
Last active November 28, 2017 15:49
Show Gist options
  • Save Axure/aa473345c79d85d00b1335137361b09b to your computer and use it in GitHub Desktop.
Save Axure/aa473345c79d85d00b1335137361b09b to your computer and use it in GitHub Desktop.
GraphQL with decorators.
export const rootMutationType = new GraphQLObjectType({
name: 'MutationRoot',
fields: () => ({
addWord1: {
args: {
value: {
name: 'value',
type: new GraphQLNonNull(GraphQLString),
},
},
type: wordType,
async resolve(obj: any, {value}: {
value: string;
}, context: any) {
const newWord = new Word();
newWord.value = value;
return await newWord.save();
},
},
})
})
export const rootQueryType = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
args: {
value: {
name: 'value',
type: GraphQLString,
},
},
resolve() {
return 'world';
},
},
shit: {
type: new GraphQLObjectType({
name: 'shit',
fields: {
shit: {
type: GraphQLString,
args: {
name: {
name: 'name',
type: GraphQLString
}
},
resolve(obj: any, {name}: any, context: any) {
return obj.shit + context.msg + ', name:' + name;
},
},
},
}),
resolve(obj: any, {}, context: any) {
context.msg = '_context';
return {
shit: 'shit',
};
},
},
},
});
class TestRootMutations {
@GraphQLRootMutationField(wordType, {
value: {
name: 'value',
type: new GraphQLNonNull(GraphQLString),
},
})
async addWord1(obj: any, {value}: {
value: string;
}, context: any) {
const newWord = new Word();
newWord.value = value;
return await newWord.save();
}
}
@GraphQLObjectDefinition
class ShitQuery implements GraphQLQueryDescriptionInterface<ShitQueryInterface, ShitQuery> {
@GraphQLQueryField(GraphQLString, {
name: {
name: 'name',
type: GraphQLString
}
})
shit(obj: any, params: { name: string }, context: any): string {
return obj.shit + context.msg + ', name:' + params.name;
}
}
@GraphQLRootQuery
class ShitRootQuery {
@GraphQLRootQueryField(GraphQLString, {
value: {
type: GraphQLString,
}
})
hello(obj: any, params: {
value: string;
}, context: any): string {
return 'world!' + params.value;
}
@GraphQLRootQueryLazyField(() => ShitQuery)
shit(obj: any, params: any, context: any): ShitQueryInterface {
context.msg = '_context';
return {
shit: 'shit'
};
}
}
query {
getWordWithExplanations(word: "shit") {
value, wordExplanations {
id, word {
value, wordExplanations {
content, word {
wordExplanations {
content, word {
wordExplanations {
word {
value
createdAt
updatedAt
}
}
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment