GraphQL buildSchema vs GraphQLObjectType
Asked Answered
D

1

12

I went through GraphQL's Object Types tutorial and then read through the Constructing Types part of the docs. I did a similar style trial by creating a simplecase convention converter. Why? To learn :)

When converting to using GraphQLObjectType, I wanted the same results as buildSchema.

  1. Why does buildSchema use type CaseConventions but when using GraphQLObjectType it is not set at a type? Am I doing something wrong here?
  2. Did I implement this with any alarming problems?
  3. Should I be using a rootValue object with the GraphQLObjectType version as I did with the buildQuery version?

Thank you for your patience and help.


Both versions use this Object:

class CaseConventions {

  constructor(text) {
    this.text = text;
    this.lowerCase = String.prototype.toLowerCase;
    this.upperCase = String.prototype.toUpperCase;
  }

  splitTargetInput(caseOption) {
    if(caseOption)
      return caseOption.call(this.text).split(' ');
    return this.text.split(' ');
  }

  cssCase() {
    const wordList = this.splitTargetInput(this.lowerCase);
    return wordList.join('-');
  }

  constCase() {
    const wordList = this.splitTargetInput(this.upperCase);
    return wordList.join('_');
  }

}

module.exports = CaseConventions; 

buildSchema version:

const schema = new buildSchema(`
  type CaseConventions {
    cssCase: String
    constCase: String
  }
  type Query {
    convertCase(textToConvert: String!): CaseConventions
  }
`);

const root = {
  convertCase: ({ textToConvert }) => {
    return new CaseConventions(textToConvert);
  }
};

app.use('/graphql', GraphQLHTTP({
  graphiql: true,
  rootValue: root,
  schema
}));

GraphQLObjectType version:

const QueryType = new GraphQLObjectType({
  name: 'Query',
  fields: {
    cssCase: {
      type: GraphQLString,
      args: { textToConvert: { type: GraphQLString } },
      resolve(parentValue) {
        return parentValue.cssCase();
      }
    },
    constCase: {
      type: GraphQLString,
      args: { textToConvert: { type: GraphQLString } },
      resolve(parentValue) {
        return parentValue.constCase()
      }
    }
  }
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    convertCase: {
      type: QueryType,
      args: { textToConvert: { type: GraphQLString } },
      resolve(p, { textToConvert }) {
        return new CaseConventions(textToConvert);
      }
    }
  }
});

const schema = new GraphQLSchema({
  query: RootQuery
});

app.use('/graphql', GraphQLHTTP({
  graphiql: true,
  schema
}));
Deaden answered 26/6, 2017 at 17:30 Comment(0)
W
12

I will try to answer you question satisfactorily.

  1. Why does buildSchema use type CaseConventions but when using GraphQLObjectType it is not set at a type? Am I doing something wrong here

    They are two different ways of implementation. Using buildSchema uses the graphQL schema language while GraphQLSchema does not use the schema language, it creates the schema programmatically.

  2. Did I implement this with any alarming problems?

    Nope

  3. Should I be using a rootValue object with the GraphQLObjectType version as I did with the buildQuery version?

    No, In buildSchema, the root provides the resolvers while in using GraphQLSchema, the root level resolvers are implemented on the Query and Mutation types rather than on a root object.

Woodsum answered 21/7, 2017 at 1:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.