ardatan/graphql-toolkit

Property 'fields' does not exist on type 'ScalarTypeDefinitionNode'.

lubiikpupiik opened this issue · 4 comments

When i log Schema.Type.astNode i see the fields array of the fields on the type, but the fields property isn't included in the type definition. Maybe this is not the right approach, how to get the fields of the type? But i think, when the fields array is in the returned value, it should be in the type definition. ✌️

@lubiikpupiik Could you elaborate your problem? Which feature/module of graphql-toolkit are you using? What do you need? etc

@ardatan Oh 😄 Sorry. ✌️ its about @graphql-toolkit/core. Specifically about getTypeMap method in the returned value of loadSchema function. Here is the source code:

import { GraphQLFileLoader } from '@graphql-toolkit/graphql-file-loader';
import { loadSchema } from '@graphql-toolkit/core';

loadSchema('src/test.graphql', {
  loaders: [
    new GraphQLFileLoader()
  ]
}).then(value => {
  const typeMap = value.getTypeMap()
  console.log(typeMap.TestType.astNode.fields)
})

Problem is in the fields property (typeMap.TestType.astNode.fields), which is not specified in the ScalarTypeDefinitionNode type, but it exists in the returned value of getTypeMap method (returned type of getTypeMap method is ScalarTypeDefinitionNode). So right now it will throw an error: Property 'fields' does not exist on type 'ScalarTypeDefinitionNode'.

@lubiikpupiik I think this error is thrown by TS Compiler Property 'fields' does not exist on type 'ScalarTypeDefinitionNode'.. So it is correct, a scalar type definition shouldn't have fields like most of types other than input, interface and object. We use GraphQLSchema from graphql-js together with all those DefinitionNode typings. You might have seen fields in a ScalarDefinitionNode but it would be an empty array probably; because scalar types don't have fields as in TS typings of them :)

@ardatan Yeah it seems right. I probably using the package incorrectly. I'll describe more, what i am trying to do. I am creating the package for graphql error handling. For example. I have graphql file, which looks like this:

type UserInputErrorField {
  key: String!
  message: String!
}

type UserInputError {
  message: String
  fields: [UserInputErrorField!]!
}

And from it. I want to generate something like this:

interface UserInputErrorField {
  key: string;
  message: string;
}

class UserInputError extends MyGraphqlError {
  message?: string;
  fields: UserInputErrorField[];

  constructor (message?: string, field: UserInputErrorField[]) {
    super('USER_INPUT_ERROR');

    this.message = message;
    this.fields = fields;
  }
}

And right now, i am not sure what is the proper way to get my types. Like... the ScalarTypeDefinitionNode is obviously wrong, but the typeMap object shows me even the other types, than the scalars and it has also the ScalarTypeDefinitionNode type. I guess i should handle this in different way. But can you please tell me, how i should get the types of the fields from the gql file?