【问题标题】:Recommended way for sharing types between frontend and nodejs graphql在前端和 nodejs graphql 之间共享类型的推荐方式
【发布时间】:2020-06-26 17:53:46
【问题描述】:

我已经开始在 nodejs 上使用带有 graphql 的 typescript。但让我印象深刻的一件事是使用 apollo-server-express 中的gql。有没有办法可以在其他地方使用 graphql 模式中使用的类型。

import { gql } from 'apollo-server-express';

export const userSchema = gql`
  type User {
    id: String!
    name: String
  }

  type Query {
    getUsers: [User]
  }
`;

现在我不想为User 创建单独的接口。并希望有如下界面可供使用:

export const usersResolver = {
  Query: {
    getUsers: () => db.get(condition).then(result => {
      const users:Array<User> = [];
    }
  }
};

另外,我也想在前端使用这些类型。

如何做到这一点。

【问题讨论】:

    标签: node.js typescript graphql frontend


    【解决方案1】:

    您应该使用GraphQL Code Generator 之类的东西来根据您的架构和查询自动为您生成类型。

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2021-08-02
      • 1970-01-01
      • 2021-03-08
      • 2022-11-20
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多