简体   繁体   English

GraphQLError:语法错误:预期名称,找到“)”

[英]GraphQLError: Syntax Error: Expected Name, found ")"

I keep getting this error supposedly coming from typeDefs我一直收到这个错误,据说来自 typeDefs

import gql from 'graphql-tag';

const typeDefs = gql`
  type CustomerService {
    findAll(): [CustomerEntity]
    findOne(id: String!): CustomerEntity
    createCustomer(input: CreateCustomerInput!): CustomerEntity
    updateCustomer(id: String!, input: UpdateCustomerInput!): CustomerEntity
    deleteCustomer(id: String!): Boolean
  }

  type Query {
    customers: [CustomerEntity]
    customer(id: String!): CustomerEntity
  }

  type Mutation {
    createCustomer(input: CreateCustomerInput!): CustomerEntity
    updateCustomer(id: String!, input: UpdateCustomerInput!): CustomerEntity
    deleteCustomer(id: String!): Boolean
  }

  type CustomerEntity {
    id: String
    name: String
    email: String
  }

  input CreateCustomerInput {
    name: String!
    email: String!
  }

  input UpdateCustomerInput {
    name: String
    email: String
  }
`;

export default typeDefs;

here goes my service这是我的服务

import { Injectable } from '@nestjs/common';
import { CustomerEntity } from './entities/customer.entity';
import { CreateCustomerInput } from './dto/create-customer.input';
import { UpdateCustomerInput } from './dto/update-customer.input';
import { Sales } from '../sales/entity/sales.entity';

@Injectable()
export class CustomerService {
  private customers: CustomerEntity[] = [];

  async createCustomer(input: CreateCustomerInput): Promise<CustomerEntity> {
    const customer = new CustomerEntity();
    customer.id = (this.customers.length + 1).toString();
    customer.firstName = input.firstName;
    customer.lastName = input.lastName;
    customer.email = input.email;
    customer.password = input.password;
    customer.sales = [new Sales()];
    this.customers.push(customer);
    return customer;
  }

  async findAll(): Promise<CustomerEntity[]> {
    return this.customers;
  }

  async findOne(id: string): Promise<CustomerEntity> {
    return this.customers.find((customer) => customer.id === id);
  }

  async updateCustomer(
    id: string,
    input: UpdateCustomerInput,
  ): Promise<CustomerEntity> {
    const customerIndex = this.customers.findIndex(
      (customer) => customer.id === id,
    );
    if (customerIndex === -1) {
      throw new Error('Customer not found');
    }

    const customer = this.customers[customerIndex];
    if (input.firstName) {
      customer.firstName = input.firstName;
    }
    if (input.lastName) {
      customer.lastName = input.lastName;
    }
    if (input.email) {
      customer.email = input.email;
    }
    if (input.password) {
      customer.password = input.password;
    }

    this.customers[customerIndex] = customer;
    return customer;
  }

  async deleteCustomer(id: string): Promise<boolean> {
    const customerIndex = this.customers.findIndex(
      (customer) => customer.id === id,
    );
    if (customerIndex === -1) {
      throw new Error('Customer not found');
    }

    this.customers.splice(customerIndex, 1);
    return true;
  }
}

resolver:解析器:

/* eslint-disable @typescript-eslint/no-unused-vars */
import { IResolvers } from '@graphql-tools/utils';
import { CustomerService } from './customer.service';

export class CustomerResolver implements IResolvers {
  [key: string]: any;

  constructor(private readonly customerService: CustomerService) {}

  Query = {
    customers: async (root, args, context, info) => {
      return this.customerService.findAll();
    },
    customer: async (root, { id }, context, info) => {
      return this.customerService.findOne(id);
    },
  };

  Mutation = {
    createCustomer: async (root, { input }, context, info) => {
      return this.customerService.createCustomer(input);
    },
    updateCustomer: async (root, { id, input }, context, info) => {
      return this.customerService.updateCustomer(id, input);
    },
    deleteCustomer: async (root, { id }, context, info) => {
      return this.customerService.deleteCustomer(id);
    },
  };
}

I keep getting this error:我不断收到此错误:

return new _GraphQLError.GraphQLError(`Syntax Error: ${description}`, {
         ^
GraphQLError: Syntax Error: Expected Name, found ")".

Your findAll field of the CustomerService type does not have any arguments. Hence, the parenthesis does not make sense.您的CustomerService类型的findAll字段没有任何 arguments。因此,括号没有意义。 You have to remove them.你必须删除它们。

type CustomerService {
    findAll: [CustomerEntity]
    findOne(id: String!): CustomerEntity
    createCustomer(input: CreateCustomerInput!): CustomerEntity
    updateCustomer(id: String!, input: UpdateCustomerInput!): CustomerEntity
    deleteCustomer(id: String!): Boolean
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJs TypeScript:语法错误:意外令牌,预期; - ReactJs TypeScript : Syntax error: Unexpected token, expected ; 语法错误:意外名称“实现” - Syntax Error: Unexpected Name “implements” 错误 TS2416:“ApolloError”类型中的属性“originalError”不可分配给基本类型“GraphQLError”中的相同属性 - error TS2416: Property 'originalError' in type 'ApolloError' is not assignable to the same property in base type 'GraphQLError' 清单 JSON 文件返回 404 not found 错误以及语法错误 - Manifest JSON file returning 404 not found error along with a syntax error eslint 缩进错误“预期缩进 2 个空格,但发现 4 个。”,angular 14 - eslint indent error "Expected indentation of 2 spaces but found 4.", angular 14 如何修复“错误:预计在 'ProxyZone' 中运行,但未找到。” 在摩卡测试中? - How to fix "Error: Expected to be running in 'ProxyZone', but it was not found." in mocha testing? 为什么在键中使用反引号时会出现语法错误“Property assignment expected.ts(1136)”? - why does syntax error "Property assignment expected.ts(1136)" occur when using backtick in key? 错误:打字稿发现以下错误:找不到名称“ require” - Error: Typescript found the following errors: Cannot find name 'require' tsconfig show typescript file not found error after change file name - Tsconfig show typescript file not found error after change file name 错误:NG0301:未找到名称“ngForm”的导出 - Error: NG0301: Export of name 'ngForm' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM