ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 815 forks source link

Array of Objects is returning empty Objects. #390

Closed Panoplos closed 7 years ago

Panoplos commented 7 years ago

I have a schema that I am building with makeExecutableSchema which contains a query of the following (shortened for this issue report) form:

# The Root Query
type Query {
  # Get's the user's profile with an authToken.
  profile: ClientProfile!
}

# Profile snapshot of Client
type ClientProfile {
  # The clientId
  id: ID!
  ...
  # An array of objects with information about the client's social identities
  identities: [IdentityType]
  ...
}

# The OAuth2 identity provider.
enum IdentityProviderType {
  GOOGLE
  FACEBOOK
  TWITTER
  LINKEDIN
}

# Source of Identity (when account created with OAuth2 provider)
type IdentityType {
  # Provider type
  provider: IdentityProviderType!
  # Email associated with the identity
  email: Email!
}

I have a resolver of the following form:

export default {
  Query: {
    async profile(_, {}, { authToken }) {
      /**
       * Authentication
       */
      const email = security.requireAuth(authToken)

      /**
       * Resolution
       */
      const account = await accounts.find({ email })
      if (!account) {
        throw new GraphQLError('No account was found with the given email.', GraphQLError.codes.GRAPHQL_NOT_FOUND, 400)
      }
      const identities = R.reduce((ids, id) => R.append(R.omit(['accessToken'], id), ids), [], account.identities)
      R.pipe(
        R.omit(accounts.blackList),
        R.assoc('identities', identities)
      )(account)
    }
  }
}

So, an issue that I am having is that even though this resolver is indeed returning an appropriately formed object, including the array of IdentityType's, as can be seen below:

graphql-tools array issue

The executable schema is resolving identities as an array of empty objects:

graphql-tools empty object

Panoplos commented 7 years ago

And for clarification, Email type is resolved as a GraphQLEmail in another module, so this is not the issue--I am using this in many other places without problems. I also tested changing the IdentityType to

# Source of Identity (when account created with OAuth2 provider)
type IdentityType {
  # Provider type
  provider: String!
  # Email associated with the identity
  email: String!
}

But still no go.

Panoplos commented 7 years ago

Oh my. OK, this was a really simple problem. I am not sure why, but I was expecting the query to resolve to the complete object by default (I guess because it was a simple type without its own resolver), but obviously changing the Query to

query {
  profile {
    id
    identities {
      email,
      provider
    }
  }
}

Fixes this issue... Bleh!!!

stubailo commented 7 years ago

Thanks for posting the resolution!

Panoplos commented 7 years ago

Yeah, to be honest, these kinds of solutions to gotchas are pretty useful when searching for something that may be getting you... haha.

manico commented 6 years ago

I have a similar issue. I query apps collection like this:

{
  appTemplates {
    id
  }
}

Collection is empty, but returns this:

{
  "data": {
    "appTemplates": [
      {
        "id": null
      }
    ]
  }
}

is it possible that I get only:

{
  "data": {
    "appTemplates": []
  }
}

:/