【问题标题】:How to reference generated Prisma schema in my schema.graphql file for a query that filters by id如何在我的 schema.graphql 文件中为按 id 过滤的查询引用生成的 Prisma 模式
【发布时间】:2019-11-06 10:26:30
【问题描述】:

尝试添加一个按此对象的唯一 ID 过滤的查询。

查询.js

async function getAbility (root, args, context, info) {
        return await context.prisma.ability({
        where : {id : args.abilityId}
    }, info)
}

这也在我的 schema.graphql 文件中定义。

getAbility(where: AbilityWhereUniqueInput) : Ability

我知道 AbilityWhereUniqueInput 来自使用 Prisma CLI 完成的模式生成,但我不确定如何为 schema.graphql 文件引用它。

我试图在文件顶部添加这个:

# import * from './generated/prisma-client/prisma-schema'

但每当我尝试运行该应用程序时,它都会遇到一个意外字符“.”,指的是我为导入提供的文件路径的第一部分。

其他相关声明:

schema.graphql

type Ability {
  id: ID! 
  name: String!
  description: String!
  imagePath: String!
}

【问题讨论】:

    标签: node.js graphql apollo prisma prisma-graphql


    【解决方案1】:

    prisma.yml

    # Specifies the HTTP endpoint of your Prisma API (deployed to a Prisma Demo server).
    endpoint: https://eu1.prisma.sh/public-cookiearm-769/exp-graphql-subscription/dev
    
    # Defines your models, each model is mapped to the database as a table.
    datamodel: datamodel.prisma
    
    # Specifies the language and directory for the generated Prisma client.
    generate:
      - generator: javascript-client
        output: ../src/generated/prisma-client
      - generator: graphql-schema
        output: ../src/generated/prisma.graphql
    
    # Seed your service with initial data based on `seed.graphql`.
    seed:
      import: seed.graphql
    
    # Ensures Prisma client is re-generated after a datamodel change.
    hooks:
      post-deploy:
        - prisma generate
    
    # If specified, the `secret` must be used to generate a JWT which is attached
    # to the `Authorization` header of HTTP requests made against the Prisma API.
    # Info: https://www.prisma.io/docs/prisma-graphql-api/reference/authentication-ghd4/
    # secret: mysecret123
    

    在这里你看到我正在生成两个文件。一种用于 prisma 客户端,另一种用于在 schema.graphql 中导入类型。

    schema.graphql

    # import * from './generated/prisma.graphql'
    
    type Query {
      feed: [Post!]!
      drafts: [Post!]!
      post(id: ID!): Post
    }
    
    type Mutation {
      createDraft(title: String!, content: String): Post
      editPost(id: String!, data: PostUpdateInput!): Post
      deletePost(id: ID!): Post
      publish(id: ID!): Post
    }
    
    type Subscription {
      post: Post!
    }
    
    type Post {
      id: ID!
      published: Boolean!
      title: String!
      content: String!
    }
    
    

    看到这一行

    # import * from './generated/prisma.graphql'
    

    现在我可以在 schema.graphql 中使用 PostUpdateInput 确保更改路径。

    【讨论】:

      【解决方案2】:

      确保导入的生成架构是 .graphql 扩展名,因为您无法在 graphql 文件中导入非 graphql 文件。

      【讨论】:

        猜你喜欢
        • 2020-07-07
        • 2021-12-17
        • 2020-10-10
        • 2021-08-11
        • 1970-01-01
        • 2023-03-30
        • 2019-09-06
        • 2019-03-26
        • 1970-01-01
        相关资源
        最近更新 更多