How to use the buildSchema function from graphql

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

graphql.buildSchema is a function that takes in a string representation of a GraphQL schema and returns a GraphQLSchema object.

90
91
92
93
94
95
96
97
98
99
const graphqlHTTP = require('express-graphql')
```

创建一个 `schema` 来定义查询语句和类型,`buildSchema()` 方法需要传入的参数是**字符串**类型,如下面的 `hero` 查询字段,后面的 `String` 类型表示字段返回的数据类型:   
```js
const schema = buildSchema(`
    type Query {
        hero: String
    }
`)
fork icon152
star icon890
watch icon26

+ 9 other calls in file

37
38
39
40
41
42
43
44
45
46
47
if (!onlySupportsSingleArg) {
  // graphql@16 dropped support for positional arguments.
  test('graphql.graphql(...) - positional args', function (t) {
    resetAgent(done(t))


    var schema = graphql.buildSchema('type Query { hello: String }')
    var rootValue = {
      hello () {
        return 'Hello world!'
      }
fork icon211
star icon536
watch icon296

+ 47 other calls in file

How does graphql.buildSchema work?

graphql.buildSchema is a function provided by the graphql library which allows you to create a GraphQL schema by defining types, queries, and mutations as JavaScript objects, which can then be used to execute and validate GraphQL queries against the schema.

When creating a schema using buildSchema, you define the types, queries, and mutations using a schema definition language, which is a syntax that allows you to define the structure of your data graph.

Once you've defined your schema using buildSchema, you can then use it to execute and validate GraphQL queries, either in your own application or using a GraphQL client.

64
65
66
67
68
69
70
71
72
73
74
75
76


    return user


}


var schema = buildSchema(`
  type PullRequest {
    status: Int!
    state: String!
    repo_id: String!
fork icon2
star icon3
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const { buildSchema } = require("graphql");

// Define your schema
const schema = buildSchema(`
type Query {
hello: String
}
`);

// Execute a query against the schema
const rootValue = {
  hello: () => {
    return "Hello, world!";
  },
};

const query = "{ hello }";

graphql(schema, query, rootValue).then((result) => {
  console.log(result.data); // Output: { hello: 'Hello, world!' }
});

In this example, we define a simple schema with one field hello of type String. We then define a rootValue object that defines a resolver function for the hello field. We create a query string that requests the hello field, and pass the schema, query, and root value to the graphql function. The result is a Promise that resolves to an object containing the data returned by the query.

13
14
15
16
17
18
19
20
21
22
23
24
  }
}


async function setup() {
  let schemaSource = await fs.readFile("schema.graphql", "utf-8");
  let schema = buildSchema(schemaSource);


  let resolver = {
    service: {
      name: "nodejs-service",
fork icon0
star icon0
watch icon1

+ 20 other calls in file

293
294
295
296
297
298
299
300
301
302
    }
}
if (this.props.schema) {
    // in this case it's sdl
    if (typeof this.props.schema === 'string') {
        this.setState({ schema: graphql_1.buildSchema(this.props.schema) });
        // if it's an object, it must be an introspection query
    }
    else {
        this.setState({ schema: graphql_1.buildClientSchema(this.props.schema) });
fork icon0
star icon0
watch icon2

+ 4 other calls in file

492
493
494
495
496
497
498
499
500
501

if (this.props.schema) {
  // in this case it's sdl
  if (typeof this.props.schema === 'string') {
    this.setState({
      schema: graphql_1.buildSchema(this.props.schema)
    }); // if it's an object, it must be an introspection query
  } else {
    this.setState({
      schema: graphql_1.buildClientSchema(this.props.schema)
fork icon0
star icon0
watch icon2

+ 4 other calls in file

8
9
10
11
12
13
14
15
16
17
18
const { initDb, AccountModel } = require("./db/account");
const cors = require('cors')


initDb({dbName: 'account'});


let schema = buildSchema(`
  type Contact {
    id: ID!
  }
fork icon0
star icon0
watch icon1

+ 80 other calls in file