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

Add ApolloCompilerPlugin.schemaDocumentListener() #6165

Merged
merged 2 commits into from
Sep 30, 2024

Conversation

martinbonnin
Copy link
Contributor

@martinbonnin martinbonnin commented Sep 30, 2024

class TestPlugin: ApolloCompilerPlugin {
  override fun foreignSchemas(): List<ForeignSchema> {
    return listOf(
        ForeignSchema("cache", "v0.1", definitions)
    )
  }

  override fun schemaDocumentListener(): SchemaDocumentListener {
    return object : SchemaDocumentListener {
      override fun onSchemaDocument(schema: GQLDocument, outputDirectory: File) {
        // Do some codegen here
      }
    }
  }
}

See here for an example

@martinbonnin martinbonnin requested a review from BoD as a code owner September 30, 2024 13:01
Copy link

netlify bot commented Sep 30, 2024

Deploy Preview for apollo-android-docs canceled.

Name Link
🔨 Latest commit eec3e57
🔍 Latest deploy log https://app.netlify.com/sites/apollo-android-docs/deploys/66faa33fed06f40008cd9792

@martinbonnin martinbonnin force-pushed the add-schema-document-listener branch from 57cfaee to eec3e57 Compare September 30, 2024 13:10
Copy link
Contributor

@BoD BoD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good! I'll try it on the cache repo.

@martinbonnin martinbonnin merged commit 16fa109 into main Sep 30, 2024
9 checks passed
@martinbonnin martinbonnin deleted the add-schema-document-listener branch September 30, 2024 14:10
* @param outputDirectory the compiler output directory. This directory is shared with the compiler, make sure to use a specific
* package name to avoid clobbering other files.
*/
fun onSchemaDocument(schema: GQLDocument, outputDirectory: File)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I didn't catch it before, but would it make sense to also pass the packageName configured in the service here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it out on purpose. The package name can be turing complete (because of packageNameFromFilePath, etc...). How big of an issue is it to not have it? If really needed, one can always pass it as a ACP (Apollo Compiler Plugin) argument.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. Not a big issue, but I think this means the package name will have to be repeated in most cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question - why not passing the Schema rather than the GQLDocument? To correctly look at directives I should use Schema.originalDirectiveName() right?

Copy link
Contributor Author

@martinbonnin martinbonnin Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this means the package name will have to be repeated in most cases.

What about:

apollo {
  service("service") {
    packageName.set("com.example")
    plugin(project(":cache-plugin")) {
      argument("packageName", packageName.get())
    }
  }
}

And if evolution is a concern, hide it behind a java function:

apollo {
  service("service") {
    packageName.set("com.example")
    configureCachePlugin(this, project(":cache-plugin"))
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not passing the Schema rather than the GQLDocument?

Not sure. GQLDocument is overall more stable than Schema but yea in your case, looks like you need schema. I'll amend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#6166 for passing a Schema instead of a GQLDocument

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageName.get() is totally acceptable, a dedicated fun as well 👍.

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

Successfully merging this pull request may close these issues.

2 participants