问题描述
我正在尝试学习GraphQL,但是当我尝试执行{customers {id,name}}时以及尝试使用突变或搜索特定客户时遇到错误。你能帮助我吗? 我在解析器上控制台记录数据,一切都很好,但是我想问题出在数据类型上。我该如何解决?
这是我的typeDefs.js文件:
const { gql } = require("apollo-server-express");
const typeDefs = gql`
type Customer {
id: ID!
name: String!
email: String!
age: Int!
}
type Query {
customer(id: ID!): Customer
customers: [Customer]!
}
type Mutation {
addCustomer(name: String!,email: String!,age: String!): Customer!
editCustomer(
id: ID!
name: String!
email: String!
age: String!
): Customer!
deleteCustomer(id: ID!): Customer!
}
`;
module.exports = typeDefs;
这是我的resolvers.js文件:
const axios = require("axios");
const resolvers = {
Query: {
customer: async (_,{ id }) => {
const data = await axios.get("http://localhost:3000/customers/" + id);
return data;
},customers: async () => {
const data = await axios.get("http://localhost:3000/customers/");
return data;
},},Mutation: {
addCustomer: async (_,{ name,email,age }) => {
const response = await axios.post("http://localhost:3000/customers",{
name,age,});
return response.data;
},editCustomer: async (_,{ id,name,age }) => {
const response = await axios.patch(
"http://localhost:3000/customers" + id,{
name,}
);
return response.data;
},};
module.exports = resolvers;
我的数据位于json服务器中,格式为:
{
"customers": [
{
"id": "1","name": "John Doe","email": "john@gmail.com","age": 36
},{
"id": "2","name": "Keith Wilson","email": "kieth@gmail.com","age": 50
},{
"id": "3","name": "Tom Jones","email": "tom@gmail.com","age": 23
},{
"id": "5","name": "Jen Thompson","email": "jen@gmail.com","age": 22
}
]
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)