Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL buildSchema vs GraphQLObjectType

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
}));
like image 946
Michael Bruce Avatar asked Jun 26 '17 17:06

Michael Bruce


People also ask

What is buildSchema in GraphQL?

function buildSchema. Builds a Schema object from GraphQL schema language. function printSchema. Prints the schema in a standard format.

What is GraphQLObjectType?

class GraphQLObjectType. An object type within GraphQL that contains fields. class GraphQLInterfaceType. An interface type within GraphQL that defines fields implementations will contain.

What is GraphQLScalarType?

Class GraphQLScalarTypeA scalar type is a leaf node in the graphql tree of types. This class allows you to define new scalar types.

What is makeExecutableSchema?

makeExecutableSchema(options) typeDefs is a required argument and should be a GraphQL schema language string or array of GraphQL schema language strings or a function that takes no arguments and returns an array of GraphQL schema language strings.


1 Answers

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.

like image 95
kimobrian254 Avatar answered Sep 23 '22 13:09

kimobrian254