4

I used to have an resolvers.js with an object:

export const resolvers = {
   value1: 'value';
   value2: 'value';
}

and then:

import { resolvers } from './graphqlresolvers';

now I need to build my object with variable properties

let resolvers = {}

resolvers.value1= 'value';
resolvers.value2= 'value';

export resolvers;

but i get this error: Unexpected token in export resolvers;

if i do:

export default resolvers;

works ok, but then my app behaviour get crazy.... no erros, but server app is not sending right information to app client.

Always it's problem for me have no freedom to define variables, and then export or not at the end of code. someone can explain me how i must to do?

2 Answers 2

2

You can add the properties after the export statement:

export const resolvers = {};
resolvers.value1 = 'value';
resolvers.value2 = 'value';

Live on plnkr (requires cutting-edge browser, a'la Chrome v62)

You could even do it if you needed to do the assignment later, because bindings are dynamic:

export let resolvers;
resolvers = {};
resolvers.value1 = 'value';
resolvers.value2 = 'value';

Live on plnkr (requires cutting-edge browser, a'la Chrome v62)

Sign up to request clarification or add additional context in comments.

Comments

2

You are looking for the syntax

export { resolvers as resolvers }

or short

export { resolvers }

to export a variable declared elsewhere. Of course, even when building the object dynamically, you can declare the export together with the variable:

export let resolvers = {};

resolvers.value1= 'value';
resolvers.value2= 'value';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.