如何向"error array"添加一个字符串?
How to add a string to the "error array"?
我是新手graphql.I使用Node.jsapollo-server.when我重新设置报错,添加对象报错array.but如何添加字符串而不是对象 ?.
例子
throw Error("error")
{"errors":["error]}
您可以使用 docs 中的 formatError
。
Functions to format the errors and response returned from the server, as well as the parameters to graphql execution(runQuery
)
例如
server.ts
import { ApolloServer, gql } from 'apollo-server';
import { GraphQLError } from 'graphql';
const typeDefs = gql`
type Query {
_: Boolean
}
`;
const resolvers = {
Query: {
_: () => {
throw new Error('error');
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
formatError(error: GraphQLError) {
return error.message as any;
},
});
server.listen().then(({ url }) => {
console.log(`Apollo server is listening on ${url}`);
});
客户端的 GraphQL 查询:
query {
_
}
回复:
{
"errors": [
"error"
],
"data": {
"_": null
}
}
我是新手graphql.I使用Node.jsapollo-server.when我重新设置报错,添加对象报错array.but如何添加字符串而不是对象 ?.
例子
throw Error("error")
{"errors":["error]}
您可以使用 docs 中的 formatError
。
Functions to format the errors and response returned from the server, as well as the parameters to graphql execution(
runQuery
)
例如
server.ts
import { ApolloServer, gql } from 'apollo-server';
import { GraphQLError } from 'graphql';
const typeDefs = gql`
type Query {
_: Boolean
}
`;
const resolvers = {
Query: {
_: () => {
throw new Error('error');
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
formatError(error: GraphQLError) {
return error.message as any;
},
});
server.listen().then(({ url }) => {
console.log(`Apollo server is listening on ${url}`);
});
客户端的 GraphQL 查询:
query {
_
}
回复:
{
"errors": [
"error"
],
"data": {
"_": null
}
}