我如何使用 GraphQL 处理 long Int?

新手上路,请多包涵

如您所知,GraphQL 没有像 long int 这样的数据类型。因此,每当数字大到 10000000000 时,它就会抛出这样的错误: Int cannot represent non 32-bit signed integer value: 1000000000000

为此,我知道两种解决方案:

  1. 使用标量。
 import { GraphQLScalarType } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';

const myCustomScalarType = new GraphQLScalarType({
  name: 'MyCustomScalar',
  description: 'Description of my custom scalar type',
  serialize(value) {
    let result;
    return result;
  },
  parseValue(value) {
    let result;
    return result;
  },
  parseLiteral(ast) {
    switch (ast.kind) {
    }
  }
});

const schemaString = `

scalar MyCustomScalar

type Foo {
  aField: MyCustomScalar
}

type Query {
  foo: Foo
}

`;

const resolverFunctions = {
  MyCustomScalar: myCustomScalarType
};

const jsSchema = makeExecutableSchema({
  typeDefs: schemaString,
  resolvers: resolverFunctions,
});

  1. 使用 apollo-type-bigint 包

这两种解决方案都将 big int 转换为 string ,我宁愿不使用字符串(我更喜欢数字类型)。

原文由 Harsh Patel 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

正确,在 graphQL 中没有像 bigInt 这样的概念。

您可以尝试其中之一:

  1. 将类型设置为 Float 而不是 Int 它会处理所有大的 int 值并将它们发送为 number 技术选项,尽管你在技术上没有说明 - [不喜欢 string 解决方案]
  2. 将类型设置为 String ,如您所述
  3. 如您所述,提供(npm 提供的数据类型,例如) BigInt 。它将处理 big int,但会将您的值转换为 string

支持 Graphql v16 的 npm BigInt 依赖项:

原文由 lazzy_ms 发布,翻译遵循 CC BY-SA 4.0 许可协议

Graphql 引入了标量。我正在使用 Java,因此我可以提供一些 Java 解决方案。您可以按照以下步骤实现相同的目的。

在你的 .graphqls 文件中请定义标量

scalar Long

type Movie{
movieId: String
movieName: String
producer: String
director: String
demoId : Long
}

现在你必须在你的接线中注册这个标量。

 return RuntimeWiring.newRuntimeWiring().type("Query", typeWiring -> typeWiring
                .dataFetcher("allMovies", allMoviesDataFetcher).dataFetcher("movie", movieDataFetcher)
                .dataFetcher("getMovie", getMovieDataFetcher)).scalar(ExtendedScalars.GraphQLLong).build();

现在您必须为 Long 定义标量配置,如下所示。


@Configuration
public class LongScalarConfiguration {
     @Bean
        public GraphQLScalarType longScalar() {
            return GraphQLScalarType.newScalar()
                .name("Long")
                .description("Java 8 Long as scalar.")
                .coercing(new Coercing<Long, String>() {
                    @Override
                    public String serialize(final Object dataFetcherResult) {
                        if (dataFetcherResult instanceof Long) {
                            return dataFetcherResult.toString();
                        } else {
                            throw new CoercingSerializeException("Expected a Long object.");
                        }
                    }

                    @Override
                    public Long parseValue(final Object input) {
                        try {
                            if (input instanceof String) {
                                return new Long((String) input);
                            } else {
                                throw new CoercingParseValueException("Expected a String");
                            }
                        } catch (Exception e) {
                            throw new CoercingParseValueException(String.format("Not a valid Long: '%s'.", input), e
                            );
                        }
                    }

                    @Override
                    public Long parseLiteral(final Object input) {
                        if (input instanceof StringValue) {
                            try {
                                return new Long(((StringValue) input).getValue());
                            } catch (Exception e) {
                                throw new CoercingParseLiteralException(e);
                            }
                        } else {
                            throw new CoercingParseLiteralException("Expected a StringValue.");
                        }
                    }
                }).build();
        }

}

它应该可以解决您的问题。请尝试Java相关应用程序。

原文由 Kushagra Bindal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题