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 config.graphql.path option #6458

Merged
merged 5 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sour-onions-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': minor
---

Added the config option `config.graphql.path` to configure the endpoint of the GraphQL API (default `'/api/graphql'`).
2 changes: 2 additions & 0 deletions docs/pages/docs/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Options:
These can be filtered out with `apolloConfig.formatError` if you need to process them, but do not want them returned over the GraphQL API.
- `queryLimits` (default: `undefined`): Allows you to limit the total number of results returned from a query to your GraphQL API.
See also the per-list `graphql.queryLimits` option in the [Schema API](./schema).
- `path` (default: `'/api/graphql'`): The path of the GraphQL API endpoint.
- `apolloConfig` (default: `undefined`): Allows you to pass extra options into the `ApolloServer` constructor.
- `playground` (default: `process.env.NODE_ENV !== 'production'`): If truthy, will enable the GraphQL Playground for testing queries and mutations in the browser.
To configure behaviour, pass an object of [GraphQL Playground settings](https://github.com/graphql/graphql-playground#settings). See the [Apollo docs](https://www.apollographql.com/docs/apollo-server/api/apollo-server/#constructor) for more supported options.
Expand All @@ -297,6 +298,7 @@ export default config({
graphql: {
debug: process.env.NODE_ENV !== 'production',
queryLimits: { maxTotalResults: 100 },
path: '/api/graphql',
apolloConfig: {
playground: process.env.NODE_ENV !== 'production',
introspection: process.env.NODE_ENV !== 'production',
Expand Down
1 change: 1 addition & 0 deletions examples-staging/assets-cloud/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/assets-local/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/auth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
4 changes: 0 additions & 4 deletions examples-staging/basic/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export default auth.withAuth(
provider: 'sqlite',
url: process.env.DATABASE_URL || 'file:./keystone-example.db',
},
// NOTE -- this is not implemented, keystone currently always provides a graphql api at /api/graphql
// graphql: {
// path: '/api/graphql',
// },
ui: {
// NOTE -- this is not implemented, keystone currently always provides an admin ui at /
// path: '/admin',
Expand Down
1 change: 1 addition & 0 deletions examples-staging/basic/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/ecommerce/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/embedded-nextjs/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/graphql-api-endpoint/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/playground/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/roles/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples-staging/sandbox/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/blog/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/custom-admin-ui-logo/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/custom-admin-ui-navigation/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/custom-admin-ui-pages/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/custom-field-view/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/custom-field/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/default-values/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/document-field/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/extend-graphql-schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/json/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/task-manager/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/testing/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/virtual-field/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
1 change: 1 addition & 0 deletions examples/with-auth/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ type KeystoneMeta {
}

type KeystoneAdminMeta {
apiPath: String!
enableSignout: Boolean!
enableSessionItem: Boolean!
lists: [KeystoneAdminUIListMeta!]!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type AppConfig = {
adminMetaHash: string;
fieldViews: FieldViews;
lazyMetadataQuery: DocumentNode;
apiPath: string;
};

export const getApp =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export function nextGraphQLAPIRoute(keystoneConfig: KeystoneConfig, prismaClient
connectionPromise: keystone.connect(),
});

return apolloServer.createHandler({ path: '/api/graphql' });
return apolloServer.createHandler({ path: keystoneConfig.graphql?.path || '/api/graphql' });
}
4 changes: 3 additions & 1 deletion packages/keystone/src/admin-ui/admin-meta-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const staticAdminMetaQuery = gql`
__typename
adminMeta {
__typename
apiPath
enableSignout
enableSessionItem
lists {
Expand Down Expand Up @@ -65,7 +66,7 @@ export type StaticAdminMetaQuery = { __typename?: 'Query' } & {
keystone: { __typename: 'KeystoneMeta' } & {
adminMeta: { __typename: 'KeystoneAdminMeta' } & Pick<
KeystoneAdminMeta,
'enableSignout' | 'enableSessionItem'
'apiPath' | 'enableSignout' | 'enableSessionItem'
> & {
lists: Array<
{ __typename: 'KeystoneAdminUIListMeta' } & Pick<
Expand Down Expand Up @@ -124,6 +125,7 @@ type KeystoneMeta = {

type KeystoneAdminMeta = {
__typename: 'KeystoneAdminMeta';
apiPath: Scalars['String'];
enableSignout: Scalars['Boolean'];
enableSessionItem: Scalars['Boolean'];
lists: Array<KeystoneAdminUIListMeta>;
Expand Down
4 changes: 3 additions & 1 deletion packages/keystone/src/admin-ui/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export const NavItem = ({ href, children, isSelected: _isSelected }: NavItemProp
};

const AuthenticatedItemDialog = ({ item }: { item: AuthenticatedItem | undefined }) => {
const { adminMeta } = useKeystone();
const { apiPath } = adminMeta;
const { spacing, typography } = useTheme();
return (
<div
Expand Down Expand Up @@ -96,7 +98,7 @@ const AuthenticatedItemDialog = ({ item }: { item: AuthenticatedItem | undefined
>
<Stack gap="medium" padding="large" dividers="between">
{/* FIXME: Use config.graphql.path */}
<PopoverLink target="_blank" href="/api/graphql">
<PopoverLink target="_blank" href={apiPath}>
API Explorer
</PopoverLink>
<PopoverLink target="_blank" href="https://github.com/keystonejs/keystone">
Expand Down
9 changes: 6 additions & 3 deletions packages/keystone/src/admin-ui/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type KeystoneContextType = {
visibleLists: VisibleLists;
createViewFieldModes: CreateViewFieldModes;
reinitContext: () => void;
apiPath: string;
};

const KeystoneContext = createContext<KeystoneContextType | undefined>(undefined);
Expand All @@ -34,6 +35,7 @@ type KeystoneProviderProps = {
adminMetaHash: string;
fieldViews: FieldViews;
lazyMetadataQuery: DocumentNode;
apiPath: string;
};

function InternalKeystoneProvider({
Expand All @@ -42,6 +44,7 @@ function InternalKeystoneProvider({
adminMetaHash,
children,
lazyMetadataQuery,
apiPath,
}: KeystoneProviderProps) {
const adminMeta = useAdminMeta(adminMetaHash, fieldViews);
const { authenticatedItem, visibleLists, createViewFieldModes, refetch } =
Expand Down Expand Up @@ -70,6 +73,7 @@ function InternalKeystoneProvider({
reinitContext,
visibleLists,
createViewFieldModes,
apiPath,
}}
>
{children}
Expand All @@ -84,10 +88,9 @@ export const KeystoneProvider = (props: KeystoneProviderProps) => {
() =>
new ApolloClient({
cache: new InMemoryCache(),
// FIXME: Use config.graphql.path
link: createUploadLink({ uri: '/api/graphql' }),
link: createUploadLink({ uri: props.apiPath }),
}),
[]
[props.apiPath]
);

return (
Expand Down
1 change: 1 addition & 0 deletions packages/keystone/src/admin-ui/system/createAdminMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function createAdminMeta(
) {
const { ui, lists, session } = config;
const adminMetaRoot: AdminMetaRootVal = {
apiPath: config.graphql?.path || '/api/graphql',
enableSessionItem: ui?.enableSessionItem || false,
enableSignout: session !== undefined,
listsByKey: {},
Expand Down
5 changes: 3 additions & 2 deletions packages/keystone/src/admin-ui/system/createAdminUIServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import type { KeystoneConfig, SessionStrategy, CreateContext } from '../../types
import { createSessionContext } from '../../session';

export const createAdminUIServer = async (
ui: KeystoneConfig['ui'],
config: KeystoneConfig,
createContext: CreateContext,
dev: boolean,
projectAdminPath: string,
sessionStrategy?: SessionStrategy<any>
) => {
/** We do this to stop webpack from bundling next inside of next */
const { ui, graphql } = config;
const thing = 'next';
const next = require(thing);
const app = next({ dev, dir: projectAdminPath });
Expand All @@ -20,7 +21,7 @@ export const createAdminUIServer = async (
const publicPages = ui?.publicPages ?? [];
return async (req: express.Request, res: express.Response) => {
const { pathname } = url.parse(req.url);
if (pathname?.startsWith('/_next') || pathname === '/api/graphql') {
if (pathname?.startsWith('/_next') || pathname === (graphql?.path || '/api/graphql')) {
handle(req, res);
return;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/keystone/src/admin-ui/system/getAdminMetaSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ export function getAdminMetaSchema({
const adminMeta = graphql.object<AdminMetaRootVal>()({
name: 'KeystoneAdminMeta',
fields: {
apiPath: graphql.field({
type: graphql.nonNull(graphql.String),
}),
Copy link
Member

Choose a reason for hiding this comment

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

Why provide this when you need to know it to query it?

enableSignout: graphql.field({
type: graphql.nonNull(graphql.Boolean),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/admin-ui/templates/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export const config = {
bodyParser: false,
},
};
export default apolloServer.createHandler({ path: '/api/graphql' });
export default apolloServer.createHandler({ path: initializedKeystoneConfig.graphql?.path || '/api/graphql' });
`;
3 changes: 2 additions & 1 deletion packages/keystone/src/admin-ui/templates/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export default getApp({
lazyMetadataQuery: ${JSON.stringify(getLazyMetadataQuery(graphQLSchema, adminMeta))},
fieldViews: [${allViews.map((_, i) => `view${i}`)}],
adminMetaHash: "${adminMetaQueryResultHash}",
adminConfig: adminConfig
adminConfig: adminConfig,
apiPath: "${adminMetaRootVal.apiPath}",
});
`;
// -- TEMPLATE END
Expand Down
2 changes: 1 addition & 1 deletion packages/keystone/src/admin-ui/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const writeAdminFiles = (
{
mode: 'write' as const,
src: apiTemplate,
outputPath: 'pages/api/graphql.js',
outputPath: `pages/${adminMeta.apiPath}.js`,
},
]
: []),
Expand Down
Loading