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

error ApolloError: Expected (a schema) to be a GraphQL schema #6621

Closed
stolinski opened this issue Jul 16, 2020 · 7 comments · Fixed by #6624
Closed

error ApolloError: Expected (a schema) to be a GraphQL schema #6621

stolinski opened this issue Jul 16, 2020 · 7 comments · Fixed by #6624
Assignees
Milestone

Comments

@stolinski
Copy link
Contributor

Intended outcome:

Actual outcome:

How to reproduce the issue:

Versions

After upgrading to Apollo Client 3, my SSR no longer connects via Apollo-Schema-Link. Getting the error.

error ApolloError: Expected { __validationErrors: undefined, extensions: undefined, astNode: undefined, extensionASTNodes: [], __allowedLegacyNames: [], _queryType: Query, _mutationType: Mutation, _subscriptionType: null, _directives: [@skip, @include, @deprecated], _typeMap: { Query: Query, Int: Int, Boolean: Boolean, PotentialSeries: PotentialSeries, ID: ID, String: String, Float: Float, User: User, Email: Email, Vote: Vote, Sub: Sub, Date: Date,  __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, AffiliateSalesInput: AffiliateSalesInput, Subscriptions: Subscriptions, TagInput: TagInput, ViewLog: ViewLog, ContentType: ContentType }, _possibleTypeMap: {}, _implementations: {} } to be a GraphQL schema.

Not sure what's changed here, but I'm using the proper imports

import { renderToStringWithData } from '@apollo/client/react/ssr'
import { SchemaLink } from '@apollo/client/link/schema'
import {
  ApolloClient,
  ApolloLink,
  InMemoryCache,
  ApolloProvider,
} from '@apollo/client'
          const schemaLink = new SchemaLink({
            schema,
            context: { user, userId: user ? user._id : undefined },
          })

          const client = new ApolloClient({
            ssrMode: true,
            link: schemaLink,
            cache,
          })

Not sure how the schema that I'm passing in is suddenly no longer a valid GraphQL Schema. The error only shows up in useQuery return errors. Not seeing an error anywhere else.

This same setup worked up until moving off of apollo-client rc's and moving to the new import { SchemaLink } from '@apollo/client/link/schema' import.

I don't have a reproduction because of how complex these things are, but seeing as how the same code worked with the release candidate, it seems like a bug to me.

@stolinski
Copy link
Contributor Author

I should mention that there is no other errors present and that the client hydrates correctly using the exact same schema, so it's clearly valid.

@stolinski
Copy link
Contributor Author

More information. @apollo/link-schema works but @apollo/client/link/schema does not.

@stolinski stolinski reopened this Jul 16, 2020
stolinski added a commit to stolinski/apollo-client that referenced this issue Jul 16, 2020
@apollo/client/link/schema does not work as described in the documentation where `@apollo/link-schema` does with Apollo Client v3. Even if `@apollo/client/link/schema` is the way forward, it's currently broken and anyone reading this rn will hit errors.

apollographql#6621
@benjamn benjamn self-assigned this Jul 16, 2020
@benjamn benjamn added this to the Post 3.0 milestone Jul 16, 2020
benjamn pushed a commit that referenced this issue Jul 16, 2020
@apollo/client/link/schema does not work as described in the documentation (#6621),
whereas `@apollo/link-schema` does still work with Apollo Client v3.
benjamn added a commit that referenced this issue Jul 16, 2020
This prevents the @apollo/client/link/schema CommonJS bundle (which is
used by Node.js) from accidentally pulling in the entire dependency tree
of graphql/execution/execute.

This duplication not only increased the @apollo/client/link/schema CJS
bundle size from 1.31kB to a whopping 22.6kB, but also caused instanceof
to stop working reliably for types like GraphQLSchema, because there might
be more than one definition of that constructor in play, and instanceof is
sensitive to the exact constructor function you pass as the right-hand
argument.

Should fix #6621, allowing us to revert #6622. Thanks to @stolinsky for
surfacing this issue.
@benjamn
Copy link
Member

benjamn commented Jul 16, 2020

Fix incoming: #6624

benjamn added a commit that referenced this issue Jul 16, 2020
This prevents the @apollo/client/link/schema CommonJS bundle (which is
used by Node.js) from accidentally pulling in the entire dependency tree
of graphql/execution/execute.

This duplication not only increased the @apollo/client/link/schema CJS
bundle size from 1.31kB to a whopping 22.6kB, but also caused instanceof
to stop working reliably for types like GraphQLSchema, because there might
be more than one definition of that constructor in play, and instanceof is
sensitive to the exact constructor function you pass as the right-hand
argument.

Should fix #6621, allowing us to revert #6622. Thanks to @stolinski for
surfacing this issue.
benjamn added a commit that referenced this issue Jul 16, 2020
This reverts commit 218a2b4,
now that #6621 has been fixed by #6624.
@benjamn
Copy link
Member

benjamn commented Jul 16, 2020

This should now be fixed in @apollo/[email protected]. Let us know if it isn't fixed, and we can reopen this issue.

@nicosampler
Copy link

I'm facing the same issue here, I can use the schema in the client perfectly fine but when I try to use it with SSR fails with this error:

Server Error
Error: Expected { default: { __validationErrors: undefined, extensions: undefined, astNode: [Object], extensionASTNodes: [], __allowedLegacyNames: [], _queryType: Query, _mutationType: Mutation, _subscriptionType: undefined, _directives: [Array], _typeMap: [Object], _possibleTypeMap: {}, _implementations: {} } } to be a GraphQL schema.

My code:

function createIsomorphLink() {
  if (typeof window === 'undefined') {
    const { SchemaLink } = require('@apollo/client/link/schema');
    const schema = require('../../graphql/schema');
    return new SchemaLink({ schema });
  } else {
    const { HttpLink } = require('@apollo/client/link/http');
    return new HttpLink({
      uri: '/api/graphql',
      credentials: 'same-origin',
    });
  }
}

schema:

Object [Module] {
  default: GraphQLSchema {
    __validationErrors: undefined,
    extensions: undefined,
    astNode: {
      kind: 'SchemaDefinition',
      directives: [],
      operationTypes: [Array],
      loc: [Object]
    },
    extensionASTNodes: [],
    __allowedLegacyNames: [],
    _queryType: Query,
    _mutationType: Mutation,
    _subscriptionType: undefined,
    _directives: [ @skip, @include, @deprecated ],
    _typeMap: [Object: null prototype] {
      Query: Query,
      User: User,
      ID: ID,
      String: String,
      Role: Role,
      Boolean: Boolean,
      Zone: Zone,
      DateTime: DateTime,
      Client: Client,
      IdType: IdType,
      ClientAddress: ClientAddress,
      Int: Int,
      ContactType: ContactType,
      ClientPhone: ClientPhone,
      Loan: Loan,
      LoanType: LoanType,
      Float: Float,
      LoanStatus: LoanStatus,
      Periodicity: Periodicity,
      Mutation: Mutation,
      CreateUserInput: CreateUserInput,
      SignInInput: SignInInput,
      SignInPayload: SignInPayload,
      ClientInput: ClientInput,
      ClientAddressInput: ClientAddressInput,
      ClientPhoneInput: ClientPhoneInput,
      LoanInput: LoanInput,
      Date: Date,
      ZoneInput: ZoneInput,
      __Schema: __Schema,
      __Type: __Type,
      __TypeKind: __TypeKind,
      __Field: __Field,
      __InputValue: __InputValue,
      __EnumValue: __EnumValue,
      __Directive: __Directive,
      __DirectiveLocation: __DirectiveLocation,
      Time: Time,
      PaymentStatus: PaymentStatus,
      PaymentType: PaymentType
    },
    _possibleTypeMap: [Object: null prototype] {},
    _implementations: [Object: null prototype] {}
  }
}

dependencies:

"@apollo/client": "^3.1.4",
"apollo-link-schema": "^1.2.5",

@fromi
Copy link

fromi commented Nov 9, 2020

Hi @nicosampler, I have exactly the same issue: using SchemaLink the schema gets wrapped into a {default: schema} object which don't pass the schema validation.
I need SchemaLink for SSR at build time (next.js).
Did you find a workaround?

@fromi
Copy link

fromi commented Nov 9, 2020

I just found the reason why it was wrapped with {default: ...}:
const schema = require('./schema') is not equivalent to import schemafrom './schema'.
You need to do const schema = require('./schema').default

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants