-
Hope Code like this: const schema: { typeDefs: any, resolvers: any } = gql`
type Person {
_id: ID! ${async (root, args, ctx, info) => {
return root._id;
}}
name: String!
books(name_like: String!): [Book!]! ${async (root, args, ctx, info) => {
return db.manyOrNone(`select * from book where person_id=? and name like ?`, [root._id, args.name_like]);
}}
}
` And infact I have implemented one: export function make_schema(strings: TemplateStringsArray, ...values: any[]) {
let typeDefs = '', resolvers = {};
strings.forEach((str, idx) => {
typeDefs += str;
let value = values[idx];
let type_or_fields = typeDefs.match(/((scalar|type)\s+\w+)|(\w+(\(.+\))?:)/g);
type_or_fields.reverse();
let is_field = type_or_fields[0].endsWith(':');
let type_name = type_or_fields.find(it => it.startsWith('scalar') || it.startsWith('type')).split(/\s+/)[1];
if (is_field) {
let field_name = type_or_fields[0].match(/\w+/)[0];
resolvers = { ...resolvers, [type_name]: { ...resolvers[type_name], [field_name]: value } };
// resolvers[type_name][field_name] = value;
} else {
resolvers = { ...resolvers, [type_name]: value };
// resolvers[type_name] = value;
}
});
return { typeDefs, resolvers };
}
export const schema = {typeDefs: '', resolvers: {}};
export const gql = (strings: TemplateStringsArray, ...values: any[]) => {
let s = make_schema(strings, ...values);
schema.typeDefs += '\n' + s.typeDefs;
lodash.merge(schema.resolvers, s.resolvers);
return s;
}; WIth this, we will never come across the Error of having a resolver without corresponding schema. |
Beta Was this translation helpful? Give feedback.
Answered by
xialvjun
May 9, 2019
Replies: 1 comment 1 reply
-
If posssible, hope it can generate types, const schema: { typeDefs: any, resolvers: any } = gql`
type Person {
_id: ID! ${async (root, args, ctx, info) => {
return root._id;
}}
name: String!
books(name_like: String!): [Book!]! ${async (root, args: gql.Person.books.args, ctx, info) => {
return db.manyOrNone(`select * from book where person_id=? and name like ?`, [root._id, args.name_like]);
}}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ardatan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If posssible, hope it can generate types,
args: gql.Person.books.args
: