3

I was going through the documentation for GraphQl and realized that the new Schema Langugage supports only default resolvers. Is there a way I can add custom resolvers while using the new Schema Language?

let userObj = {
  id: 1,
  name: "A",
  homeAddress: {
    line1: "Line1",
    line2: "Line2",
    city: "City"
  }
};

let schema = buildSchema(`
  type Query {
    user(id: ID): User
  }

  type User {
    id: ID
    name: String
    address: String 
  }
`);

//I would like User.address to be resolved from the fields in the json response eg. address = Line1, Line2, City

This is the schema that I have defined. I would like to add some behavior here that would allow me to parse the address object and return a concatenated string value.

2 Answers 2

2

As mentioned by HagaiCo and in this github issue, the right way would be to go with graphql-tools.

It has a function called makeExecutableSchema, which takes a schema and resolve functions, and then returns an executable schema

Sign up to request clarification or add additional context in comments.

Comments

0

It seems like you have a confusion in here, since you defined that address is String but you send a dictionary to resolve it.

what you can do, is to define a scalar address type: scalar AddressType if you use buildSchema and then attach parse functions to it. (or use graphql-tools to do it easily)

or build the type from scratch like shown in the official documentations:

var OddType = new GraphQLScalarType({
  name: 'Odd',
  serialize: oddValue,
  parseValue: oddValue,
  parseLiteral(ast) {
    if (ast.kind === Kind.INT) {
      return oddValue(parseInt(ast.value, 10));
    }
    return null;
  }
});

function oddValue(value) {
  return value % 2 === 1 ? value : null;
}

and then you can parse the dictionary into a string (parseValue) and otherwise

5 Comments

Apologies. I have update the question by renaming the 'address' field in the object to 'homeAddress'. So I want that object to resolve into a string. My question is more around how to achieve that with the new schema language
the new buildSchema still needs to get resolvers attached into the schema, it will define the skeleton but it still needs to know how to parse/deparse/resolve fields
Yeah that I am aware of. Just wondering if there is way to achieve using the new buildSchema. I like the new syntax as it seems much cleaner.
as far as i know, you need to iteterate on the fields on the schema and update thier resolve/parse/etc.. you can see example in here: github.com/apollostack/graphql-tools/blob/master/src/… or you can use graphql-tools instead and it will do it for you using that function :)
Had a look at that repo as well. Would probably go along those lines only if there is no other solution. Upvoting the above comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.