如何使用 GraphQL 处理 long Int?

Har*_*tel 6 javascript bigint node.js graphql

如您所知,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,
});
Run Code Online (Sandbox Code Playgroud)
  1. 使用apollo-type-bigint 包

他们都将 big int 转换为string,这不是完美的解决方案。

请如果你知道然后帮助我。如何将大整数作为数字而不是字符串传递?

小智 10

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

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

scalar Long

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

Run Code Online (Sandbox Code Playgroud)

现在您必须在接线中注册该标量。

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

Run Code Online (Sandbox Code Playgroud)

现在您必须为 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();
        }

}

Run Code Online (Sandbox Code Playgroud)

它应该可以解决你的问题。Java相关应用请尝试一下。


laz*_*_ms 6

是的,有没有像概念bigIntgraphQL

您可以尝试其中之一,

  1. 集合类型Float,而不是Int 将处理所有的大型国际价值,也为发送number

  2. 如果数据类型无关紧要,那么您也可以提供 BigInt 数据类型。它还将处理 big int 但会将您的值转换为string

  3. 同样,数据类型对您来说不是问题,然后您可以选择 npm 来处理您的情况

npm 链接:

https://www.npmjs.com/package/apollo-type-bigint

https://www.npmjs.com/package/graphql-bigint

https://www.npmjs.com/package/graphql-type-bigint

  • “*`Float` 将处理所有大 int 值*” - 直到 53 位,即。 (3认同)