graphql#GraphQLObjectType JavaScript Examples

The following examples show how to use graphql#GraphQLObjectType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: types.js    From crate with MIT License 6 votes vote down vote up
CrateType = new GraphQLObjectType({
  name: 'crate',
  description: 'Crate Type',

  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
    description: { type: GraphQLString },
    createdAt: { type: GraphQLString },
    updatedAt: { type: GraphQLString }
  })
})
Example #2
Source File: types.js    From crate with MIT License 6 votes vote down vote up
ProductType = new GraphQLObjectType({
  name: 'product',
  description: 'Product Type',

  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
    slug: { type: GraphQLString },
    type: { type: GraphQLInt },
    gender: { type: GraphQLInt },
    description: { type: GraphQLString },
    image: { type: GraphQLString },
    createdAt: { type: GraphQLString },
    updatedAt: { type: GraphQLString }
  })
})
Example #3
Source File: types.js    From crate with MIT License 6 votes vote down vote up
ProductTypesType = new GraphQLObjectType({
  name: 'productTypesType',
  description: 'User Types Type',

  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString }
  })
})
Example #4
Source File: types.js    From crate with MIT License 6 votes vote down vote up
SubscriptionType = new GraphQLObjectType({
  name: 'subscription',
  description: 'Subscription Type',

  fields: () => ({
    id: { type: GraphQLInt },
    user: { type: UserType },
    crate: { type: CrateType },
    createdAt: { type: GraphQLString },
    updatedAt: { type: GraphQLString }
  })
})
Example #5
Source File: types.js    From crate with MIT License 6 votes vote down vote up
UserType = new GraphQLObjectType({
  name: "user",
  description: "User type",

  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
    email: { type: GraphQLString },
    address: { type: GraphQLString },
    password: { type: GraphQLString },
    role: { type: GraphQLString },
    createdAt: { type: GraphQLString },
    updatedAt: { type: GraphQLString },
  }),
})
Example #6
Source File: types.js    From crate with MIT License 6 votes vote down vote up
UserLoginType = new GraphQLObjectType({
  name: "userAuth",
  description: "User Authentication Type",

  fields: () => ({
    user: { type: UserType },
    token: { type: GraphQLString },
  }),
})
Example #7
Source File: types.js    From crate with MIT License 6 votes vote down vote up
UserGenderType = new GraphQLObjectType({
  name: "userGender",
  description: "User Gender Type",

  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
  }),
})
Example #8
Source File: mutations.js    From crate with MIT License 6 votes vote down vote up
mutation = new GraphQLObjectType({
  name: 'mutations',
  description: 'API Mutations [Create, Update, Delete]',

  fields: {
    ...user,
    ...product,
    ...crate,
    ...subscription
  }
})
Example #9
Source File: queries.js    From crate with MIT License 6 votes vote down vote up
query = new GraphQLObjectType({
  name: 'query',
  description: 'API Queries [Read]',

  fields: () => ({
    ...user,
    ...product,
    ...crate,
    ...subscription
  })
})
Example #10
Source File: App.js    From Lambda with MIT License 6 votes vote down vote up
schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world';
        },
      },
    },
  }),
})
Example #11
Source File: 107-test.js    From cybsec with GNU Affero General Public License v3.0 5 votes vote down vote up
remoteSchema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      users: {
        type: new GraphQLList(new GraphQLObjectType({
          name: 'User',
          fields: {
            name: {
              type: GraphQLString
            },
            age: {
              type: GraphQLInt
            },
            access: {
              type: new GraphQLObjectType({
                name: 'Access',
                fields: {
                  msg: {
                    type: GraphQLString
                  }
                }
              }),
              resolve: source => ({
                msg: source.age >= 20 ? `allowed` : 'disallowed'
              })
            }
          }
        })),
        resolve: () => [{
          name: 'u1',
          age: 10
        }, {
          name: 'u2',
          age: 20
        }, {
          name: 'u3',
          age: 30
        }]
      }
    }
  })
})
Example #12
Source File: 107-test.js    From cybsec with GNU Affero General Public License v3.0 4 votes vote down vote up
describe('github issue #107 merge Schema types on GQL', () => {
  it('get QueryTC from remote schema', () => {
    const RemoteQueryType = remoteSchema._queryType;
    const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);
    expect(RemoteQueryTC.getTypeName()).toBe('Query');
    expect(RemoteQueryTC.getFieldNames()).toEqual(['users']); // remoteMutationTC = ObjectTypeComposer.create(remoteSchema._mutationType);
    // remoteSubscriptionTC = ObjectTypeComposer.create(remoteSchema._subscriptionType);
  });
  it('get nested TC from remote schema', () => {
    const RemoteQueryType = remoteSchema._queryType;
    const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);
    const RemoteUserTC = RemoteQueryTC.get('users');
    expect(RemoteUserTC.getTypeName()).toEqual('User');
    const RemoteAccessTC = RemoteQueryTC.get('users.access');
    expect(RemoteAccessTC.getTypeName()).toEqual('Access');
  });
  it('schema stiching on Query',
  /*#__PURE__*/
  _asyncToGenerator(function* () {
    const RemoteQueryType = remoteSchema._queryType;
    const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);
    schemaComposer.Query.addFields(_objectSpread({
      tag: {
        type: schemaComposer.createObjectTC(`type Tag { id: Int, title: String}`),
        resolve: () => ({
          id: 1,
          title: 'Some tag'
        })
      }
    }, RemoteQueryTC.getFields()));
    expect(schemaComposer.Query.getFieldNames()).toEqual(['tag', 'users']);
    const schema = schemaComposer.buildSchema();
    expect((yield graphql(schema, `
          query {
            tag {
              id
              title
            }
            users {
              age
            }
          }
        `))).toEqual({
      data: {
        tag: {
          id: 1,
          title: 'Some tag'
        },
        users: [{
          age: 10
        }, {
          age: 20
        }, {
          age: 30
        }]
      }
    });
  }));
  it('schema stiching on Query.remote',
  /*#__PURE__*/
  _asyncToGenerator(function* () {
    const RemoteQueryType = remoteSchema._queryType;
    const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);
    schemaComposer.Query.addFields({
      tag: {
        type: schemaComposer.createObjectTC(`type Tag { id: Int, title: String}`),
        resolve: () => ({
          id: 1,
          title: 'Some tag'
        })
      },
      remote: {
        type: schemaComposer.createObjectTC({
          name: 'RemoteSchema',
          fields: RemoteQueryTC.getFields()
        }),
        resolve: () => ({}) // it's important to return something (not null/undefined)

      }
    });
    expect(schemaComposer.Query.getFieldNames()).toEqual(['tag', 'remote']);
    const schema = schemaComposer.buildSchema();
    expect((yield graphql(schema, `
          query {
            tag {
              id
              title
            }
            remote {
              users {
                age
              }
            }
          }
        `))).toEqual({
      data: {
        tag: {
          id: 1,
          title: 'Some tag'
        },
        remote: {
          users: [{
            age: 10
          }, {
            age: 20
          }, {
            age: 30
          }]
        }
      }
    });
  }));
  it('using remote type in local schema',
  /*#__PURE__*/
  _asyncToGenerator(function* () {
    const RemoteQueryType = remoteSchema._queryType;
    const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);
    const RemoteUserTC = RemoteQueryTC.getFieldTC('users');
    const remoteUsersFC = RemoteQueryTC.getFieldConfig('users');
    const LocalArticleTC = schemaComposer.createObjectTC({
      name: 'Article',
      fields: {
        text: {
          type: 'String'
        },
        author: {
          type: RemoteUserTC,
          args: _objectSpread({}, remoteUsersFC.args),
          resolve: (source, args, context, info) => {
            if (!remoteUsersFC.resolve) return null;
            const users = remoteUsersFC.resolve(source, args, context, info); // for simplicity return first user

            return users[0];
          }
        }
      }
    });
    schemaComposer.Query.addFields({
      article: {
        type: LocalArticleTC,
        resolve: () => ({
          text: 'Article 1'
        })
      }
    });
    const schema = schemaComposer.buildSchema();
    expect((yield graphql(schema, `
          query {
            article {
              text
              author {
                name
                age
                access {
                  msg
                }
              }
            }
          }
        `))).toEqual({
      data: {
        article: {
          text: 'Article 1',
          author: {
            access: {
              msg: 'disallowed'
            },
            age: 10,
            name: 'u1'
          }
        }
      }
    });
  }));
  it('adding remote type to SchemaComposer and check reference by name', () => {
    const RemoteQueryType = remoteSchema._queryType;
    const RemoteQueryTC = schemaComposer.createObjectTC(RemoteQueryType);
    const UserTC = RemoteQueryTC.getFieldTC('users');
    schemaComposer.add(UserTC);
    const ArticleTC = schemaComposer.createObjectTC({
      name: 'Article',
      fields: {
        user: 'User',
        users: ['User']
      }
    });
    const userType = ArticleTC.getFieldType('user');
    expect(userType).toBeInstanceOf(GraphQLObjectType);
    expect(userType.name).toBe('User');
    const usersType = ArticleTC.getFieldType('users');
    expect(usersType).toBeInstanceOf(GraphQLList);
    expect(usersType.ofType.name).toBe('User');
  });
});