GraphQL Node教程第7步遇类型重定义等语法错误,求指导
Hey there! Let's break down your issues step by step to get you back on track with the GraphQL subscriptions chapter:
1. Fixing the "Redefining Type" Error
The root cause of those duplicate type errors is straightforward:
You've defined User, Link, and Vote in both your datamodel.prisma and Schema.graphql files. Prisma automatically generates a GraphQL schema (usually in generated/prisma-client/schema.graphql) based on your datamodel. When you run your server, it tries to merge this generated schema with your custom Schema.graphql—and hits conflicts because the types exist twice.
Solution:
Remove the duplicate type definitions from your Schema.graphql. You only need to keep your custom operations (queries, mutations, subscriptions) and any extra types/enums you've added (like Feed, AuthPayload, and LinkOrderByInput). Here's what your cleaned-up Schema.graphql should look like:
type Query { info: String! feed(filter: String, skip: Int, first: Int, orderBy: LinkOrderByInput): Feed! } type Feed { links: [Link!]! count: Int! } type AuthPayload { token: String user: User } type Subscription { newLink: Link newVote: Vote } type Mutation { post(url: String!, description: String!): Link! signup(email: String!, password: String!, name: String!): AuthPayload login(email: String!, password: String!): AuthPayload vote(linkId: ID!): Vote } enum LinkOrderByInput { description_ASC description_DESC url_ASC url_DESC createdAt_ASC createdAt_DESC }
2. Are Your Directives & Scalars in datamodel.prisma Correct?
Absolutely! Those directives and scalars are part of Prisma's datamodel syntax (not standard GraphQL), and they're working exactly as intended:
@id: Marks a field as the primary key for the database model@unique: Enforces a unique constraint on the field in the database@createdAt: Automatically sets the field to the timestamp when the record is created (you never need to manually assign this value)DateTime: A Prisma-provided scalar that maps to date/time types in your database
The official tutorial might not explicitly cover these because they're standard Prisma configuration—you're using them correctly here.
3. Quick Post-Fix Checks
To make sure everything works smoothly:
- Run
prisma generatein your terminal to refresh the generated Prisma client and schema - Double-check your server code is merging the Prisma-generated schema with your custom
Schema.graphql(most setups usemergeSchemasor pass an array of typeDefs to Apollo Server) - Restart your GraphQL server and test the subscriptions functionality
内容的提问来源于stack exchange,提问作者jackdaw

