簡體   English   中英

GraphQLError:語法錯誤:預期名稱,找到“)”

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

我一直收到這個錯誤,據說來自 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;

這是我的服務

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;
  }
}

解析器:

/* 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);
    },
  };
}

我不斷收到此錯誤:

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

您的CustomerService類型的findAll字段沒有任何 arguments。因此,括號沒有意義。 你必須刪除它們。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM