Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additionalResolvers example for passing args to nested fields #779

Open
rboyd opened this issue Jul 30, 2020 · 4 comments
Open

Add additionalResolvers example for passing args to nested fields #779

rboyd opened this issue Jul 30, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@rboyd
Copy link

rboyd commented Jul 30, 2020

I am trying to use it to wrap a remote schema in a container running https://github.com/graphql-compose/graphql-compose-elasticsearch (graphql queries to elasticsearch).

For my case I need to restrict the indices that a client can query. I'm stuck on how to use additionalResolvers to pass args to a nested field.

Here's a query example against target: https://imgur.com/a/tDVT8bC and my attempt so far: https://gist.github.com/rboyd/f67d5efc03221bb936926fdc0d33201f (line 6 where I try elastic.api.elastic.search) I'm not sure where to find docs or impl of what the api function expects for args.

@ardatan
Copy link
Owner

ardatan commented Jul 30, 2020

@rboyd Could you create a minimal reproduction so we can work on it and make sure it works before asking you to test it ? :)

@ardatan ardatan added the bug Something isn't working label Jul 30, 2020
@fredericomba
Copy link

fredericomba commented Jul 30, 2020

@rboyd, I've taken a look at your JavaScript snippet, and there is a problem with it. It has syntax errors. I've written a version of your script that does not have syntax errors and runs fine on Node.js, and you can see the attached screenshots for clarification.

RUNNING ORIGINAL CODE: https://ibb.co/t3Cxk3n
RUNNING MODIFIED CODE: https://ibb.co/0Chf97c

Here is the modified code:

const resolvers = {
  Query: {
    restrictedSearch: async (root, args, { elastic }) => {

      const { results } = await elastic.api.elastic.search({
        index: args.index,
        q: args.q,
      });

      return results;

    },
  },
};

module.exports = { resolvers };

@rboyd
Copy link
Author

rboyd commented Jul 30, 2020

@Frederico-Miranda thank you -- with a graphql query:

query MySearch {
  restrictedSearch(q: "test", index:"none") 
}

I get:

  "errors": [
    {
      "message": "elastic.api.elastic.search is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "restrictedSearch"
      ]
    }
  ],
  "data": {
    "restrictedSearch": null
  }
}

here is my .meshrc.yaml:

sources:
  - name: elastic
    handler:
      graphql:
        endpoint: http://localhost:9494/
additionalTypeDefs: |
  extend type Query {
    restrictedSearch(index: String!, q: String!): JSON
  }
additionalResolvers:
  - ./src/mesh/additional-resolvers.js

I'm using docker image https://hub.docker.com/r/mewil/elastic-graphql

partial docker-compose:

  search:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
    environment:
    - discovery.type=single-node
    - "ES_JAVA_OPTS=-Xms750m -Xmx750m"
    ports:
    - "9200:9200"
    - "9300:9300"
  elastic-graphql:
    image: mewil/elastic-graphql:latest@sha256:c44f7f1b2633a69f4afc945246808d85495ae9c2b9b7f3df1c03f30c6ccbbfad
    environment:
    - ELASTIC_GRAPHQL_HOST=search:9200
    - ELASTIC_GRAPHQL_VERSION=7.x
    ports:
    - "9494:9494"
    links:
    - search
  graphql-mesh:
    image: rboyd/graphql-mesh
    links:
    - elastic-graphql
    volumes:
    - ./graphql-mesh/.meshrc.yaml:/work/.meshrc.yaml
    ports:
    - 4000:4000

@ardatan
Copy link
Owner

ardatan commented Aug 11, 2020

@rboyd Could you create a repo?

@theguild-bot theguild-bot mentioned this issue Aug 11, 2022
@theguild-bot theguild-bot mentioned this issue Sep 28, 2023
This was referenced Apr 30, 2024
This was referenced May 7, 2024
klippx pushed a commit to klippx/graphql-mesh that referenced this issue Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants