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

Custom query resolver is always null #12

Closed
4F2E4A2E opened this issue Feb 20, 2018 · 4 comments
Closed

Custom query resolver is always null #12

4F2E4A2E opened this issue Feb 20, 2018 · 4 comments

Comments

@4F2E4A2E
Copy link

4F2E4A2E commented Feb 20, 2018

Ho can the query method be passed to the right Query type parent?
Or how can the resolver class be annotated correctly in order to resolve?

.graphql

type CustomQuery{
    foo: String
}
type Query {
    mw: CustomQuery
}

CustomResolver

import { Query, Resolver } from '@nestjs/graphql';

@Resolver()
export class CustomResolver {
  constructor() {}

  @Query()
  foo(): string {
    return 'bar';
  }
}

create schema

const typeDefs = this.graphQLFactory.mergeTypesByPaths( './**/*.graphql');
const schema = this.graphQLFactory.createSchema({ typeDefs });

result

{
  "data": {
    "mw": {
      "foo": null
    }
  },
  "extensions": {
    "tracing": {
      "version": 1,
      "startTime": "2018-02-20T17:01:09.202Z",
      "endTime": "2018-02-20T17:01:09.202Z",
      "duration": 242964,
      "execution": {
        "resolvers": [
          {
            "path": [
              "mw"
            ],
            "parentType": "Query",
            "fieldName": "mw",
            "returnType": "CustomQuery",
            "startOffset": 77828,
            "duration": 96395
          },
          {
            "path": [
              "mw",
              "foo"
            ],
            "parentType": "CustomQuery",
            "fieldName": "foo",
            "returnType": "String",
            "startOffset": 214124,
            "duration": 8691
          }
        ]
      }
    }
  }
}

maybe relevant dependencies

{
  "dependencies": {
    "@nestjs/common": "4.6.4",
    "@nestjs/core": "4.6.4",
    "@nestjs/graphql": "2.0.0",
    "@types/graphql": "0.12.4",
    "graphql": "0.13.1",
    "graphql-tools": "2.21.0"
  } 
}

A) What am I doing wrong?
B) Can anyone confirm that custom query types are not supported at the moment?
C) Would a PR supporting this via annotation be welcomed?

@rgolea
Copy link

rgolea commented Feb 23, 2018

@4F2E4A2E Okay... So you're doing wrong the entire resolver.
For the schema:

type CustomQuery{
    foo: String
}
type Query {
    mw: CustomQuery
}

You should do this:

import { Query, Resolver, ResolveProperty } from '@nestjs/graphql';

@Resolver('CustomQuery')
export class CustomResolver {
  constructor() {}
  //resolving the property
  @ResolveProperty()
  foo(): string {
    return 'bar';
  }
  //resolving via query (do one either/or)
  @Query()
  mw(){
     return {foo: 'bar'}
  }
}

@4F2E4A2E
Copy link
Author

I did ghave tried this before, it dit not work. Did you get this working for sure?

@rgolea
Copy link

rgolea commented Feb 23, 2018

Yes. The Query decorator appends the resolver to the Query type and the (as so the Mutation or the Subscription decorator). The ResolveProperty gives format to that property and the Resolver decorator NEEDS a type name.

@lock
Copy link

lock bot commented Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants