如何用graphql-tools使用或解析枚举类型?[英] How to use or resolve enum types with graphql-tools?

本文是小编为大家收集整理的关于如何用graphql-tools使用或解析枚举类型?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我找不到graphql-tools文档中的任何地方,如何利用enum在makeExecutableSchema中使用enum类型.有人知道这是怎么做的?

示例代码:

enum Color {
  RED
  GREEN
  BLUE
}

type Car {
  color: Color!
}

Color的解析器是什么样的?

推荐答案

您不会为Color编写解析器.这是一个简单,可运行的示例:

const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('graphql-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const app = require('express')();

const carsData = [
  {color: 'RED'},
  {color: 'GREEN'},
  {color: 'BLUE'},
];

const typeDefs = `
  enum Color {
    RED
    GREEN
    BLUE
  }
  type Car {
    color: Color!
  }
  type Query {
    cars: [Car!]!
  }
`;

const resolvers = {
  Query: {
    cars: () => carsData,
  }
};

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));

app.listen(3000);

graphiql 中,您会看到每种颜色返回的颜色我们的数据中的汽车.现在,将数据中的值之一更改为您未定义的颜色,例如PINK.再次运行查询,您将看到一个错误消息,例如:

"Expected a value of type \"Color\" but received: PINK"

这也与解析器一起使用,因此,如果我通过为这样的汽车添加解析器来覆盖数据:

Car: {
  color: () => 'RED'
}

查询将以红色为颜色显示所有汽车.如果将解析器返回的值更改为BLACK,则查询将再次错误.

枚举只是强制执行的一种方式,即特定字段所解决的任何值都在您定义的值集内.

其他推荐答案

默认情况下,枚举用相同的字符串表示:enum Color { RED }是'RED'. 您可以通过在枚举中添加解析器来覆盖此问题:

Color: {
  RED: '#ff0000',
  GREEN: '#00ff00'
},
Query {...

更多信息:/docs/graphql-tools/scalars.html#internal-values

本文地址:https://www.itbaoku.cn/post/1938152.html

问题描述

I cannot find anywhere in the graphql-tools documentation how one should go about utilizing enum types in schemas that are fed to makeExecutableSchema. Anyone have a clue how this done?

Example code:

enum Color {
  RED
  GREEN
  BLUE
}

type Car {
  color: Color!
}

What would the resolver for Color look like?

推荐答案

You wouldn't write a resolver for Color. Here's a simple, runnable example:

const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('graphql-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const app = require('express')();

const carsData = [
  {color: 'RED'},
  {color: 'GREEN'},
  {color: 'BLUE'},
];

const typeDefs = `
  enum Color {
    RED
    GREEN
    BLUE
  }
  type Car {
    color: Color!
  }
  type Query {
    cars: [Car!]!
  }
`;

const resolvers = {
  Query: {
    cars: () => carsData,
  }
};

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));

app.listen(3000);

Run a cars query ({cars {color}}) in GraphiQL and you will see a color returned for each car in our data. Now, change one of the values in the data (not the Enum definition) to a color you didn't define, like PINK. Run the query again and you will see an error message like:

"Expected a value of type \"Color\" but received: PINK"

This works with resolvers too, so if I override the data by adding a resolver for Car like this:

Car: {
  color: () => 'RED'
}

The query will show all the cars with RED as their color. If you change the value returned by the resolver to BLACK, the query will error out again.

Enums are just a way of enforcing that whatever value a particular field resolves to is within the set of values you define.

其他推荐答案

By default, the enum is represented with the same string : enum Color { RED } is 'RED'. You can override this by adding a resolver to the enum:

Color: {
  RED: '#ff0000',
  GREEN: '#00ff00'
},
Query {...

More info: https://www.apollographql.com/docs/graphql-tools/scalars.html#internal-values