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

Federation: Add support for federationMetadata for executable schemas #6

Open
gdorsi opened this issue Mar 22, 2022 · 3 comments
Open

Comments

@gdorsi
Copy link

gdorsi commented Mar 22, 2022

Hello!

As for now the federationMetadata can be enabled only on string schemas.

Would be useful to support also "executable" schemas.

@gdorsi gdorsi changed the title Federation: Add support for federationMetadata for non string schemas Federation: Add support for federationMetadata for executable schemas Mar 22, 2022
@mcollina
Copy link
Contributor

I would like to do have that too! Would you like to send a PR?

@gdorsi
Copy link
Author

gdorsi commented Mar 23, 2022

Yes I'd like to, but it could take a while because ATM I don't have so much time to work on it.

If somebody else picks it up in the meantime, that's fine for me.

@xxSkyy
Copy link

xxSkyy commented Mar 23, 2022

I've made a dirty solution for this problem to provide _service { sdl } query for executable schema as gateway needs. Then applied middlewares works aswell as gateway reads schema and passing queries.

const { makeExecutableSchema } = require("@graphql-tools/schema")
const fs = require("fs")

const makeGatewayExecutableSchema = async ({
  schemaPath,
  schemaString,
  resolvers,
}) => {
  const serviceInfo = `
  type Service {
    sdl: String
  }

  type Query {
    _service: Service
  }
  type Mutation {
    _service: Service
  }
  type Subscription {
    _service: Service
  }
  `

  let schema

  if (schemaPath) {
    try {
      schema = await fs.readFileSync(schemaPath, "utf-8")
    } catch (e) {
      console.log(e)
      throw Error("Can't load schema file")
    }
  } else {
    schema = schemaString
  }

  // TODO: check if there are type query and swap to extend
  if (
    schema.indexOf("extend type Query") === -1 &&
    schema.indexOf("type Query") !== -1
  ) {
    throw Error(
      `You can't have 'type Query' in your schema, 
      only 'extend type Query' and so on with mutations and subscriptions`
    )
  }

  resolvers.Query._service = () => {
    return { sdl: `${schema}` }
  }

  return makeExecutableSchema({
    typeDefs: serviceInfo + schema,
    resolvers,
  })
}

Edit: Updated, it should work fine for many services, I've also made temporary module mercurius-exec-gateway-fix till it will get proper fix

@codeflyer codeflyer transferred this issue from mercurius-js/mercurius Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants