graphql#GraphQLList JavaScript Examples

The following examples show how to use graphql#GraphQLList. 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: 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 #2
Source File: query.js    From crate with MIT License 5 votes vote down vote up
crates = {
  type: new GraphQLList(CrateType),
  args: {
    orderBy: { type: GraphQLString }
  },
  resolve: getAll
}
Example #3
Source File: query.js    From crate with MIT License 5 votes vote down vote up
products = {
  type: new GraphQLList(ProductType),
  resolve: getAll
}
Example #4
Source File: query.js    From crate with MIT License 5 votes vote down vote up
productsRelated = {
  type: new GraphQLList(ProductType),
  args: {
    productId: { type: GraphQLInt }
  },
  resolve: getRelated
}
Example #5
Source File: query.js    From crate with MIT License 5 votes vote down vote up
productTypes = {
  type: new GraphQLList(ProductTypesType),
  resolve: getTypes
}
Example #6
Source File: query.js    From crate with MIT License 5 votes vote down vote up
subscriptions = {
  type: new GraphQLList(SubscriptionType),
  resolve: getAll
}
Example #7
Source File: query.js    From crate with MIT License 5 votes vote down vote up
subscriptionsByUser = {
  type: new GraphQLList(SubscriptionType),
  resolve: getByUser
}
Example #8
Source File: query.js    From crate with MIT License 5 votes vote down vote up
users = {
  type: new GraphQLList(UserType),
  resolve: getAll
}
Example #9
Source File: query.js    From crate with MIT License 5 votes vote down vote up
userGenders = {
  type: new GraphQLList(UserGenderType),
  resolve: getGenders
}
Example #10
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');
  });
});