简体   繁体   English

通过 id/email 在 graphQL 中查找一个元素

[英]Finding one element in graphQL by id/email

I'm trying to make a findOneByEmail/id for graphQL.我正在尝试为 graphQL 创建一个 findOneByEmail/id。 I saw a couple of questions, blog posts, and videos, but they didn't help my case.我看到了一些问题、博客文章和视频,但它们对我的情况没有帮助。 I know I have to add a filter, but I have to be missing something我知道我必须添加一个过滤器,但我必须遗漏一些东西

Here are my resolvers这是我的resolvers

const users = [
  {id: 1, email:'a@a.a',password:'zaq1@WSX', pons:[{value:'test'}]},
  {id: 2, email:'b@b.b',password:'ZAQ!2wsx', pons:[{value:'tset'}]}
];
const pons = [{value: 'test'}];
module.exports = {
  Query: {
    users: () => users,
    pons: () => pons,
  }
};

typeDefs

const {gql} = require('apollo-server-express');

module.exports = gql`  
  type Pon {
      value: String!
  }
  type User {
    id: Int
    email: String!
    password: String!
    pons: [Pon]!
  }
  type Query {
      findUser(id: Int): [User]
      users: [User]
      pons: [Pon]
  }
`;

app.js

const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const typeDefs = require('./graphql/typeDefs.js');
const resolvers = require('./graphql/resolvers.js');

const server = new ApolloServer({typeDefs, resolvers});

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
  console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);

I am using express with apollo-server-express .我正在使用expressapollo-server-express

I tried adding users(id: Int) and users(email: String) , but with no success.我尝试添加users(id: Int)users(email: String) ,但没有成功。 You can see it in findUser query.您可以在findUser查询中看到它。 I am calling the query like:我正在调用这样的查询:

query{
  findUser(id: 1) {
    email
    pons {
      value
    }
  }
}

In the GQL playground.在 GQL 操场上。

I'd like to able to filter the data on the server, not on the client, and can't find the solution anywhere我希望能够过滤服务器上的数据,而不是客户端上的数据,并且在任何地方都找不到解决方案

You can get the id parameters in GraphQL resolver and query user by it.您可以在 GraphQL 解析器中获取id参数并通过它查询用户。

Eg例如

const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { gql } = require('apollo-server-express');

const users = [
  { id: 1, email: 'a@a.a', password: 'zaq1@WSX', pons: [{ value: 'test' }] },
  { id: 2, email: 'b@b.b', password: 'ZAQ!2wsx', pons: [{ value: 'tset' }] },
];
const pons = [{ value: 'test' }];

const typeDefs = gql`
  type Pon {
    value: String!
  }
  type User {
    id: Int
    email: String!
    password: String!
    pons: [Pon]!
  }
  type Query {
    findUser(id: Int): User
    users: [User]
    pons: [Pon]
  }
`;

const resolvers = {
  Query: {
    users: () => users,
    pons: () => pons,
    findUser: (_, { id }) => users.find((u) => u.id === id),
  },
};

const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () => console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`));

query in GraphQL playground:在 GraphQL 操场上查询:

query{
  findUser(id: 1){
    email 
    pons {
      value
    }
  }
}

response:回复:

{
  "data": {
    "findUser": {
      "email": "a@a.a",
      "pons": [
        {
          "value": "test"
        }
      ]
    }
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM