Skip to content

Commit

Permalink
feat: change host to gqlHost and add rest host
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Klesse committed Sep 3, 2024
1 parent 17230d6 commit bcb5d6b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion playground/nuxt-app/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default defineNuxtConfig({

nuxtBase: {
generateTypes: process.env['GENERATE_TYPES'] === '1',
host: 'http://localhost:3000/graphql',
gqlHost: 'http://localhost:3000/graphql',
host: 'http://localhost:3000',
registerAuthPlugins: true,
schema: '../server/schema.gql',
storagePrefix: 'playground',
Expand Down
8 changes: 4 additions & 4 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export async function loadMetaServer(config: Partial<{ public: { host: string; s
let schema;

if (!config.public.schema) {
if (!config.public.host) {
throw new Error('Host is not defined in the configuration');
if (!config.public.gqlHost) {
throw new Error('Gpql Host is not defined in the configuration');
}

const { data: result } = await ofetch(config.public.host, {
const { data: result } = await ofetch(config.public.gqlHost, {
body: JSON.stringify({
query: getIntrospectionQuery({ descriptions: false }),
variables: {},
Expand Down Expand Up @@ -207,7 +207,7 @@ export async function generateFiles(options: any, logger: any, nuxt: any, resolv
const meta = await loadMetaServer({ public: options });

// Generate graphql types
const generatedTypes = await generateGraphQLTypes(options.schema ?? options.host);
const generatedTypes = await generateGraphQLTypes(options.schema ?? options.gqlHost);
addTemplate({
filename: nuxt.options.rootDir + '/src/base/default.ts',
getContents: () => generatedTypes[0].content || '',
Expand Down
7 changes: 5 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface ModuleOptions {
autoImport?: boolean;
disableGraphql?: boolean;
generateTypes?: boolean;
gqlHost: string;
host: string;
registerAuthPlugins?: boolean;
registerPlugins?: boolean;
Expand Down Expand Up @@ -38,6 +39,7 @@ export default defineNuxtModule<ModuleOptions>({
autoImport: false,
disableGraphql: false,
generateTypes: true,
gqlHost: '',
host: '',
registerAuthPlugins: false,
registerPlugins: true,
Expand All @@ -57,8 +59,9 @@ export default defineNuxtModule<ModuleOptions>({
const runtimeDir = resolver.resolve('runtime');
nuxt.options.build.transpile.push(runtimeDir);

const wsUrl = options.host?.replace('https://', 'wss://').replace('http://', 'ws://');
const wsUrl = options.gqlHost?.replace('https://', 'wss://').replace('http://', 'ws://');
nuxt.options.runtimeConfig.public['host'] = options.host ?? 'http://localhost:3000';
nuxt.options.runtimeConfig.public['gqlHost'] = options.gqlHost ?? 'http://localhost:3000/graphql';
nuxt.options.runtimeConfig.public['wsUrl'] = wsUrl ?? 'ws://localhost:3000';
nuxt.options.runtimeConfig.public['schema'] = options.schema ?? null;
nuxt.options.runtimeConfig.public['storagePrefix'] = options.storagePrefix ?? null;
Expand Down Expand Up @@ -146,7 +149,7 @@ export default defineNuxtModule<ModuleOptions>({
await installModule(await resolver.resolvePath('nuxt-graphql-request'), {
clients: {
default: {
endpoint: options.host,
endpoint: options.gqlHost,
options: {
responseMiddleware,
},
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/functions/graphql-meta.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { buildClientSchema, getIntrospectionQuery } from 'graphql';
import { ofetch } from 'ofetch';

import { GraphQLMeta } from '../../generate';
import type { GraphQLMeta } from '../../generate';

import { useGraphQLMeta } from '../composables/use-graphql-meta';

export async function loadMeta(config: Partial<{ public: { host: string; schema?: string } }>): Promise<GraphQLMeta> {
const controller = new AbortController();
setTimeout(() => controller.abort(), 5000);

return new Promise(async (resolve, reject) => {
const { data: result } = await ofetch(config.public.host, {
const { data: result } = await ofetch(config.public.gqlHost, {
body: JSON.stringify({
query: getIntrospectionQuery({ descriptions: false }),
variables: {},
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/plugins/auth.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default defineNuxtPlugin({

let token = accessTokenState.value;
if (isTokenExpired(accessTokenState.value)) {
const refreshTokenResult = await ofetch(config.public.host, {
const refreshTokenResult = await ofetch(config.public.gqlHost, {
body: JSON.stringify({
query: 'mutation refreshToken {refreshToken {token, refreshToken}}',
variables: {},
Expand Down Expand Up @@ -50,7 +50,7 @@ export default defineNuxtPlugin({
$graphql.default.setHeaders({ authorization: `Bearer ${token}` });

if (token && payload?.id) {
const userResult = await ofetch(config.public.host, {
const userResult = await ofetch(config.public.gqlHost, {
body: JSON.stringify({
query: 'query getUser($id: String!) { getUser(id: $id) { id avatar firstName lastName email roles }}',
variables: {
Expand Down

0 comments on commit bcb5d6b

Please sign in to comment.