How to use the GraphQLObjectType function from graphql

Find comprehensive JavaScript graphql.GraphQLObjectType code examples handpicked from public code repositorys.

GraphQLObjectType is a function provided by the graphql library that creates a new GraphQL object type based on the configuration options passed to it.

80
81
82
83
84
85
86
87
88
    meta.type = graphql.GraphQLID
  }
  fields[field.camelcase] = meta
}

const type = new graphql.GraphQLObjectType({
  name: entityName,
  fields
})
fork icon85
star icon888
watch icon16

+ 65 other calls in file

2
3
4
5
6
7
8
9
10
11
const fetch = require('node-fetch');
const Nightmare = require('nightmare');

const element = require('./element');

const site = new graphql.GraphQLObjectType({
  name: 'Site',
  fields: {
    select: {
      args: element.args,
fork icon48
star icon635
watch icon13

How does graphql.GraphQLObjectType work?

GraphQLObjectType is a class in the GraphQL.js library that defines a type of object in a GraphQL schema by specifying its name, description, fields, and other properties. It takes an object as an argument where the keys define the fields of the object type, and the values are objects that define the type of the field, including its name, type, and description. It also allows for the definition of input and output types for the field. The GraphQLObjectType can also define interfaces and their implementation for the type. Once created, the GraphQLObjectType instance can be used to build the GraphQL schema.

146
147
148
149
150
151
152
153
154
155
```js
const { GraphQLObjectType, GraphQLSchema, GraphQLList, GraphQLInt, GraphQLString } = require('graphql');
const { attributeFields, resolver } = require('graphql-sequelize');
const { Db, User } = require('./db');

userType = new GraphQLObjectType({
  name: 'User',
  description: 'A user',
  fields: attributeFields(User)
});
fork icon54
star icon304
watch icon13

+ 7 other calls in file

2
3
4
5
6
7
8
9
10
11
const _ = require('lodash');

const InputObjectType = graphql.GraphQLInputObjectType;
const InterfaceType = graphql.GraphQLInterfaceType;
const ScalarType = graphql.GraphQLScalarType;
const ObjectType = graphql.GraphQLObjectType;
const UnionType = graphql.GraphQLUnionType;
const NonNullType = graphql.GraphQLNonNull;
const EnumType = graphql.GraphQLEnumType;
const ListType = graphql.GraphQLList;
fork icon13
star icon203
watch icon10

+ 3 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { GraphQLObjectType, GraphQLString, GraphQLInt } = require("graphql");

const UserType = new GraphQLObjectType({
  name: "User",
  description: "Represents a user",
  fields: () => ({
    id: { type: GraphQLInt },
    name: { type: GraphQLString },
    email: { type: GraphQLString },
    age: { type: GraphQLInt },
  }),
});

module.exports = UserType;

In this example, GraphQLObjectType is used to define a UserType object type with four fields (id, name, email, and age) that correspond to their respective scalar types (GraphQLInt and GraphQLString). The fields property is a function that returns an object containing the fields of the UserType.

7
8
9
10
11
12
13
14
15
16
    applyAlone: {
        type: Boolean,
    },
})

const graphqlSchema = new GraphQLObjectType({
    name: 'TeamOptions',
    fields: () => ({
        applyAsTeam: {
            type: GraphQLBoolean,
fork icon35
star icon49
watch icon6

127
128
129
130
131
132
133
134
135
136
    }
  }
})

var schema = new graphql.GraphQLSchema({
  query: new graphql.GraphQLObjectType({
    name: 'Query'
    fields: {
      film: {
        type: Film,
fork icon34
star icon639
watch icon14

+ 7 other calls in file

3
4
5
6
7
8
9
10
11
12
var graphql = require('graphql');
var GraphQLString = require('graphql').GraphQLString;

var groupsDomain = require('../domain/groups');

const queryType = new graphql.GraphQLObjectType({
    name: 'Query',
    fields: () => ({
        group: {
            type: groupType,
fork icon43
star icon139
watch icon16

+ 5 other calls in file

132
133
134
135
136
137
138
139
140
expect.hasAssertions();

const entityName = "TestObjectList";
const slug = "test-object-list";
const type = new GraphQLList(
  new GraphQLObjectType({
    name: entityName,
  }),
);
fork icon25
star icon107
watch icon0

65
66
67
68
69
70
71
72
73
74

function isScalarType(type) {
  return type instanceof gql.GraphQLScalarType;
}
function isObjectType(type) {
  return type instanceof gql.GraphQLObjectType;
}
function isEnumType(type) {
  return type instanceof gql.GraphQLEnumType;
}
fork icon7
star icon43
watch icon3

+ 9 other calls in file

61
62
63
64
65
66
67
68
69
70
新建`schema.js`文件,首先定义两个数据模型:LaunchType(发射)和 RocketType(火箭)。注意字段的数据类型需要使用GraphQL定义的,不能使用js中的基本数据类型。

```js
const { GraphQLObjectType, GraphQLInt, GraphQLString, GraphQLBoolean, GraphQLList, GraphQLSchema } = require('graphql');

const LaunchType = new GraphQLObjectType({
  name: 'Launch',
  fields: () => ({
    flight_number: { type: GraphQLInt },
    mission_name: { type: GraphQLString },
fork icon9
star icon18
watch icon2

+ 7 other calls in file

12
13
14
15
16
17
18
19
20
21
        rocket: { type: RocketType },
    })
});

// Rocket Type
const RocketType = new GraphQLObjectType({
    name: 'Rocket',
    fields: () => ({
        rocket_id: { type: GraphQLString },
        rocket_name: { type: GraphQLString },
fork icon23
star icon16
watch icon5

+ 5 other calls in file

17
18
19
20
21
22
23
24
25
26
    if (type) fields[serviceName] = { type, resolve }
  }

  if (Object.keys(fields).length === 0) return

  return new GraphQLObjectType({ name: 'Mutation', fields })
}

const _serviceToObjectType = service => {
  const fields = {}
fork icon3
star icon23
watch icon8

+ 5 other calls in file

-2
fork icon2
star icon26
watch icon0

+ 2 other calls in file

28
29
30
31
32
33
34
35
36
37
38
39
    }
);




// Define a custom Quiz type
const QuizType = new GraphQLObjectType(
    {
        name: 'Quiz',
        description: 'Quiz Type',
        fields: () => ({
fork icon5
star icon0
watch icon0

+ 7 other calls in file

8
9
10
11
12
13
14
15
        },
        type: {
            type: new graphql.GraphQLObjectType(TitleType)
        },
        content: {
            type: new graphql.GraphQLObjectType()
        }
});
fork icon2
star icon0
watch icon27

+ 3 other calls in file

0
1
2
3
4
5
6
7
8
9
const uuidv4 = require('uuid/v4');
const _ = require("lodash");
const { GraphQLID, GraphQLInt, GraphQLFloat, GraphQLString, GraphQLBoolean, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLInputObjectType, GraphQLEnumType, GraphQLSchema } = require('graphql');
const Quotes = require('../data/quote.js');

const QuoteType = new GraphQLObjectType({
  name: 'QuoteType',
  description: 'Chuck Norris Quotes',
  fields: {
    id: { type: GraphQLString },
fork icon1
star icon5
watch icon2

+ 5 other calls in file

893
894
895
896
897
898
899
900
901
902
903
904
    }


    return fields;
};


let itemType = new graphql.GraphQLObjectType({
    name: "Item",
    fields: () => {
        let f = getItemFields(true, true);

fork icon3
star icon6
watch icon0

+ 4 other calls in file

2
3
4
5
6
7
8
9
10
11
exports.permissions = exports.schema = void 0;
const graphql_1 = require("graphql");
const graphql_middleware_1 = require("graphql-middleware");
const graphql_shield_1 = require("graphql-shield");
const schema_1 = require("./book/schema");
const query = new graphql_1.GraphQLObjectType({
    name: 'Query',
    fields: {
        ...schema_1.bookQueries,
    },
fork icon0
star icon0
watch icon1